Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

ios - AFNetworking 3.x multipart form upload

I have an upload form like this:

<form action="http://localhost/upload.php" method="post" enctype="multipart/form-data">
    <input type="file" id="upload" name="upload" />
</form>

and php code to proceed upload form:

isset($_FILES["upload"]) or die("Error");
// Path prepare stuff
if (move_uploaded_file($_FILES["upload"]["tmp_name"], $outputFile)) {
    // Other processing stuffs
}

In xcode, Im constructing the request like this:

NSMutableURLRequest* request = [[AFHTTPRequestSerializer serializer]
                                multipartFormRequestWithMethod:@"POST"
                                URLString:@"http://localhost/upload.php"
                                parameters:nil
                              constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
                                    [formData appendPartWithFormData:data name:@"somefilename.ext"];
                                } error:nil];

But seem like i did it wrong way, right?

UPDATE

Im new to AFNetworking and I want to understand how it constructs multiplart/form-data post like above. It looks like the code lack of the input's name "upload", hence will not be able to pass the first line of php upload script. I read the document from AFNetworking's GitHub, but they say nothing about constructing a form data with NSData which is the case here.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...