gpt4 book ai didi

ios - 使用 AFNetworking 上传简单的 "PUT"文件

转载 作者:行者123 更新时间:2023-11-28 17:59:08 25 4
gpt4 key购买 nike

我想这很简单,但我不知道该怎么做。我想使用 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 和表单数据将文件上传到其他网络服务,我使用了 PUT 请求(使用 AFHTTPClient 和 putPath),但我仍然不明白如何进行简单的文件上传。

感谢您的帮助!

最佳答案

我正在使用 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];

关于ios - 使用 AFNetworking 上传简单的 "PUT"文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12097415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com