无法在POST方法中在单个NSURL请求中上传2个图像

时间:2015-10-07 10:00:33

标签: php ios iphone ipad nsurlrequest

我们通过多次分离图像数据向API发送正确的帖子请求。因此,存在两个图像都没有上传的问题,只有一个图像正在上传到服务器。我检查了API团队提供的参数名称,这里是: -

#define IMAGE_DP @"Content-Disposition: form-data; name=\"profileimage\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"

#define IMAGE_COVER @"Content-Disposition: form-data; name=\"coverimage\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"

构建在单个NSURLMutable请求中上传两个图像的请求

 if ([isprofilepicupdated isEqualToString:@"1"]) {


        [postBody appendData:[IMAGE_DP dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[NSData dataWithData:profileimage]];
        [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    }
    if ([iscoverpicupdated isEqualToString:@"1"]) {
        [postBody appendData:[IMAGE_COVER dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[NSData dataWithData:coverimage]];
        [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    }

那么我们怎样才能在NSURLMutable请求的单个实体中附加两个图像?

1 个答案:

答案 0 :(得分:0)

 NSString *urlString = @"<PostImage-url>";
        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];

        NSMutableData *body = [NSMutableData data];

        NSString *boundary = @"---------------------------14737809831466499882746641449";
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
        [request addValue:contentType forHTTPHeaderField:@"Content-Type"];


        // file
        for (int i=0;i<(YOUR _ARRAY).count;i++)
        {
            UIImage * myimg=[YOUR _ARRAY objectAtIndex:i];
            NSData *ImageData = UIImagePNGRepresentation(myimg);

            [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[@"Content-Disposition: attachment; name=\"img1[]\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[NSData dataWithData:ImageData]];
            [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


        }

        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"vin\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

        // close form
        [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        // set request body
        [request setHTTPBody:body];

        //ASynchronous request

        [[NSURLConnection alloc] initWithRequest:request delegate:self];