问题
I can't understand why this is so hard. All the tutorials and articles online seem to be talking about the 1.0 api, which is pretty useless.
I've tried a few different ways and get different results. What am I doing wrong?
upload task - this seems to not be using a multipart form, wtf?
NSMutableURLRequest *request = [self.manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:url relativeToURL:[NSURL URLWithString:ApiBaseUrl]] absoluteString] parameters:@{} constructingBodyWithBlock:nil]; NSProgress *progress; NSURLSessionUploadTask *task = [self.manager uploadTaskWithRequest:request fromData:data progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { if (error) { NSLog(@"[error description] = %@", [error description]); } else { NSLog(@"success!"); } }]; [task resume];
post with a block - this seems not to attach anything
[self.manager POST:url parameters:@{} constructingBodyWithBlock:^(id <AFMultipartFormData> formData) { [formData appendPartWithFileData:data name:@"post[picture]" fileName:@"picture.jpg" mimeType:@"image/jpeg"]; } success:^(NSURLSessionDataTask *task, id response) { NSLog(@"Success"); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"Error: %@", error); }];
simple post - this seems to almost work...but not
[self.manager POST:url parameters:@{@"post[picture][]":data} success:^(NSURLSessionDataTask *task, id response) { NSLog(@"Success"); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"Error: %@", error); }];
I would love 1 to work, but I'm not sure why it doesn't.
回答1:
For a properly formed "multipart/form-data" body, you need to use use the body construction block while creating the request. Otherwise the upload task is using the raw data as the body. For example, in your AFHTTPSessionManager subclass:
NSString *urlString = [[NSURL URLWithString:kPhotoUploadPath relativeToURL:self.baseURL] absoluteString];
NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:urlString parameters:params constructingBodyWithBlock:^(id <AFMultipartFormData> formData) {
[formData appendPartWithFileData:photo.data name:@"photo" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
}];
NSURLSessionUploadTask *task = [self uploadTaskWithStreamedRequest:request progress:progress completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
if (error) {
if (failure) failure(error);
} else {
if (success) success(responseObject);
}
}];
[task resume];
Or, if you don't need to track upload progress, you can simply use:
[self POST:kPhotoUploadPath parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:photo.data name:@"photo" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(NSURLSessionDataTask *task, id responseObject) {
if (success) success(responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) failure(error);
}];
回答2:
What Ray Lillywhite describes works perfectly fine (I would've made a comment on his post, but my reputation is too low).
- Get the correct version of AFNetworking, containing this fix for updating progress when using multipart requests. At the moment of writing, that version is HEAD.
- Create a
NSMutableURLRequest
with the help ofmultipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:error:
.- Build your form data with the help of one of the
appendPartWith...
methods.
- Build your form data with the help of one of the
- Get a (upload) data task by calling the right
uploadTaskWith...
method. You NEED to useuploadTaskWithStreamedRequest:progress:completionHandler:
if you want to use theNSProgress
input parameter.
来源:https://stackoverflow.com/questions/19261253/upload-an-image-with-afnetworking-2-0