Upload image to server with iOS not working

杀马特。学长 韩版系。学妹 提交于 2019-12-04 18:21:18
Charan Giri

You missed the escaping of the CRLF characters, e.g.

Change from

[body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] 

to

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] 

The complete fix:

/*
 now lets create the body of the post
 */
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; // note: the preceding "\r\n" may be problematic if the server does not properly implement the "preamble" rule as specified in RFC 2046
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!