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.
我不明白为什么这么难。在线的所有教程和文章似乎都在谈论1.0 api,这是非常没用的。
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?
上传任务 - 这似乎不是使用多部分形式,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来工作,但我不确定为什么不工作。
2 个解决方案
#1
14
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:
对于正确形成的“multipart / form-data”主体,您需要在创建请求时使用body构造块。否则,上载任务将使用原始数据作为正文。例如,在AFHTTPSessionManager子类中:
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
5
What Ray Lillywhite describes works perfectly fine (I would've made a comment on his post, but my reputation is too low).
Ray Lillywhite所描述的作品完美无缺(我会对他的帖子发表评论,但我的名声太低了)。
- 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.
借助appendPartWith ...方法之一构建表单数据。
- 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.
获取正确版本的AFNetworking,其中包含此修复程序,用于在使用多部分请求时更新进度。在撰写本文时,该版本是HEAD。
在multipartFormRequestWithMethod的帮助下创建一个NSMutableURLRequest:URLString:parameters:constructBodyWithBlock:error:。借助appendPartWith ...方法之一构建表单数据。
通过调用正确的uploadTaskWith ...方法获取(上传)数据任务。您需要使用uploadTaskWithStreamedRequest:progress:completionHandler:如果您想使用NSProgress输入参数。
#1
14
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:
对于正确形成的“multipart / form-data”主体,您需要在创建请求时使用body构造块。否则,上载任务将使用原始数据作为正文。例如,在AFHTTPSessionManager子类中:
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
5
What Ray Lillywhite describes works perfectly fine (I would've made a comment on his post, but my reputation is too low).
Ray Lillywhite所描述的作品完美无缺(我会对他的帖子发表评论,但我的名声太低了)。
- 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.
借助appendPartWith ...方法之一构建表单数据。
- 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.
获取正确版本的AFNetworking,其中包含此修复程序,用于在使用多部分请求时更新进度。在撰写本文时,该版本是HEAD。
在multipartFormRequestWithMethod的帮助下创建一个NSMutableURLRequest:URLString:parameters:constructBodyWithBlock:error:。借助appendPartWith ...方法之一构建表单数据。
通过调用正确的uploadTaskWith ...方法获取(上传)数据任务。您需要使用uploadTaskWithStreamedRequest:progress:completionHandler:如果您想使用NSProgress输入参数。