我想这很简单,但是我不知道该怎么做。我想使用AFNetworking库将要使用PUT请求的文件上传到Web服务。这是我用来测试服务的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),但是我仍然不知道如何进行这种简单的文件上载。
谢谢你的帮助!
最佳答案
这是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];
关于ios - 使用AFNetworking轻松上传“PUT”文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12097415/