当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS里实现multipart/form-data格式上传文件

iOS里实现multipart/form-data格式上传文件

2018年09月12日  | 移动技术网IT编程  | 我要评论

海南儋州政务网,荣誉奇迹官网,caopeng超碰在线视频

ios里实现multipart/form-data格式上传文件。

http 请求(后面统一称为报文),包含请求头和请求体两部分,格式如下:

post / http/1.1
content-type:application/x-www-form-urlencoded
accept-encoding: gzip, deflate
host: w.sohu.com
content-length: 21
connection: keep-alive
cache-control: no-cache

txt1=hello&txt2=world

objective-c 代码如下:

// #define khttprequestheadcontenttypevaluemultipart @"multipart/form-data; boundary=forjoritest"
// #define khttprequestheadcontenttypekey @"content-type"
// #define khttprequestheadboundaryvalue @"forjoritest"
// #define khttprequestcontentdisposition @"content-disposition: form-data"
nsurlsessionconfiguration *configuration = [nsurlsessionconfiguration defaultsessionconfiguration];
nsurlsession *session = [nsurlsession sessionwithconfiguration:configuration];

nsurl *url = [nsurl urlwithstring:[kdetecturl stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]];
nsmutableurlrequest *mutablerequest = [nsmutableurlrequest requestwithurl:url];
mutablerequest.httpmethod = @"post";
[mutablerequest addvalue:khttprequestheadcontenttypevaluemultipart forhttpheaderfield:khttprequestheadcontenttypekey];

nsstring *body = [nsstring stringwithformat:@"--%@\r\n%@;name=\"%@\"\r\n\r\n%@", khttprequestheadboundaryvalue, khttprequestcontentdisposition, kuploadparamapikey, @"apikey"];
body = [body stringbyappendingstring:[nsstring stringwithformat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\napisecret", khttprequestheadboundaryvalue, khttprequestcontentdisposition, kuploadparamapisecret]];
body = [body stringbyappendingstring:[nsstring stringwithformat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\n1", khttprequestheadboundaryvalue, khttprequestcontentdisposition, @"return_landmark"]];
body = [body stringbyappendingstring:[nsstring stringwithformat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\ngender,age", khttprequestheadboundaryvalue, khttprequestcontentdisposition, @"return_attributes"]];
body = [body stringbyappendingstring:[nsstring stringwithformat:@"\r\n--%@\r\n%@; name=\"%@\"; filename=\"%@\" content-type=image/jpeg\r\n\r\n", khttprequestheadboundaryvalue, khttprequestcontentdisposition, kuploadparamimagefile, kuploadparamimagefile]];

nsmutabledata *data = [nsmutabledata data];
[data appenddata:[body datausingencoding:nsutf8stringencoding]];
[data appenddata:image];
[data appenddata:[[nsstring stringwithformat:@"\r\n--%@--\r\n", khttprequestheadboundaryvalue] datausingencoding:nsutf8stringencoding]];
mutablerequest.httpbody = data;
[mutablerequest setvalue:[nsstring stringwithformat:@"%lu", (unsigned long)data.length] forhttpheaderfield:@"content-length"];
nsurlsessionuploadtask *task = [session uploadtaskwithrequest:mutablerequest fromdata:nil completionhandler:^(nsdata * _nullable data, nsurlresponse * _nullable response, nserror * _nullable error) {
    }];
[task resume];

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网