如何使用AFNetworking 1.3.3形成多部分表单数据请求 [英] How to form a multipart form data request using AFNetworking 1.3.3

查看:83
本文介绍了如何使用AFNetworking 1.3.3形成多部分表单数据请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AFNetworking 1.3.3。

I am using AFNetworking 1.3.3.

我需要制作 multipart / form-data 向我的服务器发送带有图像数据和其他一些东西的请求。

I need to make a multipart/form-data request to my server with image data and some other additional stuffs.

我不知道如何格式化此多部分请求。我该怎么办?

I have no clue how to format this multipart request. How can I do it?

推荐答案

项目的GitHub页面(分支1.x),您可以找到此示例

On the project's GitHub page (branch 1.x) you can find this example

NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[httpClient enqueueHTTPRequestOperation:operation];

您还可以阅读文档

这篇关于如何使用AFNetworking 1.3.3形成多部分表单数据请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆