欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-05-01 17:43:50
ios里实现multipart/form-data格式上传文件。 http 请求(后面统一称为报文),包含请求头和请求体两部分,格式如下: post / http/1.1 content-ty...

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];