iOS上傳文件代碼,自定義組裝body

如下代碼爲上傳文件所用代碼,簡單方便,搞了很久,終於知道這麼簡單的方式來上傳。工具

其它類庫也就是把這幾句代碼封裝的亂七八糟得,讓你老久搞不懂原理。不就是在body上面加點字符串,body下面加點字符串,做爲body來傳嘛,真是,搞那麼麻煩。spa

還能夠在頭裏面加個邊界字符串,這個body也加邊界字符串,不加還省事一些。code

 

1     NSString *xmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
2     NSString *multipartString = @"Content-Disposition: form-data; name=\"fileUpload\"; file name =\"Aerial Lift Inspection Form Copy.xml\"\r\nContent-Type: text/xml\r\n\r\n";
3     NSString *httpBody = [NSString stringWithFormat:@"%@%@",multipartString,xmlString];
4     NSData *bodyData = [httpBody dataUsingEncoding:NSUTF8StringEncoding];
5     NSString *bodyLen = [NSString stringWithFormat:@"%u",bodyData.length];
6     [theRequest setHTTPBody:bodyData];

 

 

以上代碼從WireShark裏面提取出來,是Advanced Rest Client 發送文件的字符串,基本其它的工具也差很少。orm

相關文章
相關標籤/搜索