【问题标题】:Simple "PUT" file upload with AFNetworking使用 AFNetworking 进行简单的“PUT”文件上传
【发布时间】:2012-08-19 07:27:04
【问题描述】:

我想这很简单,但我不知道该怎么做。我想使用 AFNetworking 库将文件上传到使用 PUT 请求的网络服务。这是我用来测试服务的 curl 命令

mac:~ user$ curl --verbose -T image.jpeg http://server.org:8001/social/test.jpg
* About to connect() to server.org port 8001 (#0)
*   Trying 123.45.123.123...
* connected
* Connected to server.org (123.45.123.123) port 8001 (#0)
> PUT /social/test.jpg HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: server.org:8001
> Accept: */*
> Content-Length: 78341
> Expect: 100-continue
> 
< HTTP/1.1 100 CONTINUE
< Server: cx1193719-b
< Content-Type: Text/Html
< Accept-Ranges: bytes
< Content-Length: 0
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< Server: cx1193719-b
< Content-Type: Text/Html
< Accept-Ranges: bytes
< Content-Length: 0
< 
* Connection #0 to host server.org left intact
* Closing connection #0

我已经能够使用 POST 和表单数据将文件上传到其他 Web 服务,我使用了 PUT 请求(使用 AFHTTPClient 和 putPath),但我仍然不明白如何进行简单的文件上传。

感谢您的帮助!

【问题讨论】:

    标签: ios upload afnetworking put


    【解决方案1】:

    djibouti33 的回答在我看来相当不错。但是,需要注意的一件事是,答案中的 multipartFormRequest 方法已被弃用。 AFNetworking 现在建议您使用:

    - (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method
                                                  URLString:(NSString *)URLString
                                                 parameters:(NSDictionary *)parameters
                                  constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
                                                      error:(NSError *__autoreleasing *)error
    

    这里需要注意的是,您需要通过执行以下操作通过引用传递错误:

    NSError *requestError = nil;
    NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"PUT" URLString:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:photoData name:@"file" fileName:@"image" mimeType:@"image/png"];
    } error:&requestError];
    

    【讨论】:

      【解决方案2】:

      我正在使用 AFNetworking 2.0 并且想知道同样的事情。这是我最终得到的结果:

      // manager needs to be init'd with a valid baseURL
      NSURL *baseURL = [AfarHTTPSessionManager sharedManager].baseURL;
      AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];
      
      NSData *imageData = UIImageJPEGRepresentation(draftHighlight.largeImage, 1);
      
      // need to pass the full URLString instead of just a path like when using 'PUT' or 'POST' convenience methods
      NSString *URLString = [NSString stringWithFormat:@"%@%@", baseURL, _the_rest_of_your_path];
      NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"PUT" URLString:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
          [formData appendPartWithFileData:imageData name:kCreateHighlightAPIKeyImage fileName:@"highlight_image.jpg" mimeType:@"image/jpeg"];
      }];
      
      // 'PUT' and 'POST' convenience methods auto-run, but HTTPRequestOperationWithRequest just
      // sets up the request. you're responsible for firing it.
      AFHTTPRequestOperation *requestOperation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
          // success
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          // failure
      }];
      
      // fire the request
      [requestOperation start];
      

      【讨论】:

      • 请注意,由于它使用AFHTTPSessionManager,因此该解决方案仅适用于iOS7及更高版本。
      【解决方案3】:

      这是 AFNetworking 常见问题解答的一部分。检查https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ

      此外,如果您使用的是 AFHTTPClient 的子类,您可能希望使用类似于以下内容的方式对其进行扩展:

      - (void)putPath:(NSString *)path
           parameters:(NSDictionary *)parameters
                 data:(NSData*)data
              success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
              failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
      

      如此答案中所建议:https://stackoverflow.com/a/9014768/634940

      实施后,您的 AFHTTPClient 子类的基本 URL 设置为 http://server.org,您可以继续调用:

      NSData *imageData = [NSData dataWithContentsOfFile:pathToYourJPEGFile];
      [client putPath:@"social/test.jpg"
           parameters:nil
                 data:imageData
              success:yourSuccessBlock
              failure:yourFailureBlock];
      

      【讨论】:

      • 好的,我试图不去向下或子类化 httpclient,但我认为这是唯一的答案。谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多