编辑:
您上传的文件数据应使用 Base64 编码;您可以通过NSData+Base64做到这一点,然后使用:
[body appendData:[imageData1 base64EncodedString];
发送文件数据,我假设它imageData1
被定义为:
NSData* imageData = UIImagePNGRepresentation(image);
并且image
是一个UIImage
。
旧答案:
这是来自HTML 4.0.1 标准的示例(文档的最后):
Content-Type: multipart/form-data; boundary=AaB03x
--AaB03x
Content-Disposition: form-data; name="submit-name"
Larry
--AaB03x
Content-Disposition: form-data; name="files"
Content-Type: multipart/mixed; boundary=BbC04y
--BbC04y
Content-Disposition: file; filename="file1.txt"
Content-Type: text/plain
... contents of file1.txt ...
--BbC04y
Content-Disposition: file; filename="file2.gif"
Content-Type: image/gif
Content-Transfer-Encoding: binary
...contents of file2.gif...
--BbC04y--
--AaB03x--
请注意,文件在多部分/混合列表中一起列出。
因此,您可以尝试使用以下代码:
//-- new part in multipart/form-data
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"files\" dataUsingEncoding:NSUTF8StringEncoding]];
//-- new multipart/mixed
[body appendData:[[NSString stringWithFormat:@"multipart/mixed; boundary=%@", mixedBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: file; filename=\"%@\"\r\n", FileParamConstant1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData1];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", mixedBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: file; filename=\"%@\"\r\n", FileParamConstant2] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData2];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
注意更改filename
和name
属性,它应该可以工作。