如何在ios中将图像上载到服务器?
我想使用http post方法将一个图像上载到服务器。 该要求与正文中的数据类似,应作为字节流附加 我将图像转换为如何在ios中将图像上载到服务器?,ios,objective-c,file-upload,image-uploading,Ios,Objective C,File Upload,Image Uploading,我想使用http post方法将一个图像上载到服务器。 该要求与正文中的数据类似,应作为字节流附加 我将图像转换为NSData,并将该数据附加到NSUrlrequest的主体中。 但是我的状态代码是404 NSMutableData *body = [NSMutableData data]; [body appendData:[NSData dataWithData:UIImageData]]; [request setHTTPBody:body]; 但我的状态代码中出现了404错误 字节
NSData
,并将该数据附加到NSUrlrequest
的主体中。
但是我的状态代码是404
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:UIImageData]];
[request setHTTPBody:body];
但我的状态代码中出现了404错误
字节流是否与NSData相同
如果没有,那么如何使用NSURLConnection
将字节流数据发送到服务器
提前感谢。您用来将图像发送到的
URL如何
404错误代码转换为未找到
(),因此第一步应确保您使用有效的URL
尝试以这种方式进行上载:
NSData *yourData = //load your data here;
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:UPLOAD_URL]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", UPLOAD_FILE, UPLOAD_FILE] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:yourData]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
[connection start];
请显示更多的代码您的错误很可能是因为您的URL不正确,但您应该将您的图像作为正文进行Base64编码。请检查您的URL并参考此答案检查您的代码:请添加更多代码并浏览此链接