x多部分表单上传

x多部分表单上传

本文介绍了AFNetworking 3.x多部分表单上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的上传表单:

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>

和php代码继续上传表格:

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
}

在xcode中,我正在构建这样的请求:

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?

更新

我是AFNetworking的新手,我想了解它如何像上面那样构建multiplart / form-data帖子。看起来代码缺少输入的名称upload,因此无法通过php上传脚本的第一行。我从AFNetworking的GitHub上读到了这个文档,但他们没有提到用NSData构建表单数据的情况。

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.

推荐答案

那么,在 AFNetworking 3.0 你可以这样做上传多种零件数据,

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



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 3.x多部分表单上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 20:23