在IOS中使用Param数据的多部分ImageUpload

时间:2014-11-06 14:53:06

标签: ios multipart

我正在尝试使用一些文本数据上传图片。但是当我尝试仅用于图像上传时。有用!但是对于param,我得到 result = null 。所以数据发送空。这是我的代码。

-(void)uploadImage : (UIImage *)image : (NSString *)url : (NSString *)param
{
NSLog(@"URL : %@", url);
NSLog(@"param : %@", param);
NSData *myData=UIImagePNGRepresentation(image);
NSMutableURLRequest *request;
NSString *urlString = url;
NSString *filename = @"image";
request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSDictionary *temp = @{@"result"     : @"rob"
                        };
//opening boundary
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [postbody appendData:[[NSString stringWithFormat:@"--%@--", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@.png\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:myData]];
//params
[postbody appendData:[ [temp description] dataUsingEncoding:NSUTF8StringEncoding]];

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

[request setHTTPBody:postbody];

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
//  conn.delegate = self;
[conn start];

}

任何专家都可以帮我解决我错过的问题吗?

1 个答案:

答案 0 :(得分:1)

您可以使用像AFNetworking这样的库来满足您的所有要求。它非常简单,使您的代码更具可读性。这是一种使用参数(https://github.com/AFNetworking/AFNetworking#creating-an-upload-task-for-a-multi-part-request-with-progress

创建多部分文件上载的方法
NSDictionary *params = @{
    @"result" : @"rob"
}

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
    } error:nil];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSProgress *progress = nil;

NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"%@ %@", response, responseObject);
    }
}];

[uploadTask resume];