AFNetworking 3.x multipart form upload

本秂侑毒 提交于 2019-11-28 12:31:27

Well, In AFNetworking 3.0 You can do like this way for uploading multiform part data,Check this

AFNetworking 3.0 is the latest major release of AFNetworking,3.0 removes all support for the now deprecated NSURLConnection based APIs. If your project was previously using these APIs, it is recommended that you now upgrade to the NSURLSession based APIs. This guide will step you through that process.

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://localhost/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

   [formData appendPartWithFileData:data name:@"uploadFile" fileName:@"somefilename.txt" mimeType:@"text/plain"] // you file to upload

} error:nil];

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

NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
          uploadTaskWithStreamedRequest:request
          progress:^(NSProgress * _Nonnull uploadProgress) {
              // This is not called back on the main queue.
              // You are responsible for dispatching to the main queue for UI updates
              dispatch_async(dispatch_get_main_queue(), ^{
                  //Update the progress view
                  [progressView setProgress:uploadProgress.fractionCompleted];
              });
          }
          completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
              if (error) {
                  NSLog(@"Error: %@", error);
              } else {
                  NSLog(@"%@ %@", response, responseObject);
              }
          }];

[uploadTask resume];

AFNetworking doc regarding multi-part, states that you should use:

[[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST"......

and then use the NSURLSessionUploadTask for the resume method (full code in the link).

I couldn't get this to work with the server I was working on, instead I used AFHTTPSessionManager:

AFHTTPSessionManager *manager = [AFHTTPSessionManager alloc]initWithBaseURL: @"someURL..."];
// If you need to add few more headers, now is the time - if not you can skip this
[manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
[[manager requestSerializer] setValue:@"your custom value"
                    forHTTPHeaderField:@"relevant key"];
// Setting basic auth - only if you need it
[[manager requestSerializer] setAuthorizationHeaderFieldWithUsername:@"username"
                                                             password:@"password"];


             [manager POST:@"appendedPath"
                parameters:params
 constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

                   [formData appendPartWithFileData:yourNSDataFile
                                               name:@"file"
                                           fileName:@"customFileName"
                                           // The server I was working on had no type but you can google for all the existing types
                                           mimeType:@""];

             }

                 progress:nil
                  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

                                   if (responseObject != nil)
                                   {
                                       NSDictionary *jsonDictionary = responseObject;
                                   }
                               }

                   failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

                                     // Handle your error
                               }];

If you have a large file, you'll never want to do it this way, because it requires loading the whole file into memory. Here's a way to stream the file from disk into the HTTP request so that your memory usage remains low. Thanks for the original answer! It works like a charm.

NSInputStream *fileInputStream = [[NSInputStream alloc] initWithFileAtPath:filePath];

if (!fileInputStream) {
    NSLog(Error, @"Could not get a fileInputStream from the file path");
    return;
}

NSError *error;

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"PUT" URLString:fullUrlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    [formData appendPartWithInputStream:fileInputStream name:@"uniqueIdentifier" fileName:@"filename" length:<lengthOfFileLong>];

} error:&error];

if (error) {
    NSLog(Error, @"Error creating multipart form upload request: %@", [error userInfo]);
    completionHandler(nil, error);
}

[request setAllHTTPHeaderFields:headerDictionary];

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

NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
              uploadTaskWithStreamedRequest:request
              progress:^(NSProgress * _Nonnull uploadProgress) {
                  // This is not called back on the main queue.
                  // You are responsible for dispatching to the main queue for UI updates

                  NSLog(Debug, @"Cloud Upload Completion: %f", uploadProgress.fractionCompleted);
              }
              completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {

                  if (error) {
                      NSLog(@"Error: %@", [error userInfo]);
                      completionHandler(nil, error);
                  } else {
                      NSLog(@"Success: %@ %@", response, responseObject);
                  }
              }];

[uploadTask resume];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!