从我的iOS上传到我的PHP服务器的图像上挣扎
问题描述:
The code below is used to upload image to a remote server, but for a certain reason it does NOT work for me. When I enter, for example to this page and I use the php form to upload image, it works with no problem. So, concerning the PHP part, there is no single problem and everything is working.
The Question :
What's wrong with that ?
NSData *imageData = UIImageJPEGRepresentation(self.imageViewer.image, 90); NSString *urlString = @"http://example.com/post_image.php"; [request setHTTPMethod:@"POST"]; request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; NSString *boundary = @"---------------------------14737809831466499882746641449"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; [request addValue:contentType forHTTPHeaderField:@"Content-Type"]; NSMutableData *body = [NSMutableData data]; [body appendData:[[NSString stringWithFormat:@" --%@ ", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Disposition: form-data; name=\"image\"; filename=\".jpg\" " dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: application/octet-stream " dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:imageData]]; [body appendData:[[NSString stringWithFormat:@" --%@-- ", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:body]; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *picture = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; NSLog(@"uploaded picture: %@", picture);
下面的代码用于将图像上传到远程服务器,但由于某种原因它对我不起作用 。 当我进入,例如到这个页面,我使用PHP表格上传图像,它没有问题。 因此,关于PHP部分,没有一个问题,一切正常。 p> \ n
问题: h2>
这有什么问题? p>
NSData * imageData = UIImageJPEGRepresentation( self.imageViewer.image,90); NSString * urlString = @“http://example.com/post_image.php”; [请求setHTTPMethod:@“POST”]; request = [[NSMutableURLRequest alloc ] init]; [请求setURL:[NSURL URLWithString:urlString]]; [请求setHTTPMethod:@“POST”]; NSString * boundary = @“--------------------------- 14737809831466499882746641449”; NSString * contentType = [NSString stringWithFormat:@“ multipart / form-data; boundary =%@“,boundary]; [请求addValue:contentType forHTTPHeaderField:@”Content-Type“]; NSMutableData * body = [NSMutableData data]; [body appendData:[[NSString stringWithFormat:@“ - %@ ”,boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@“Content-Disposition:form-data; name = \”image \“; filename = \”。jpg \“ ”dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@“Content -Type:application / octet-stream “dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:imageData]]; [body appendData:[[NSString stringWithFormat:@” - %@ - “,boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [请求setHTTPBody:body]; NSData * returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString * picture = [[NSString alloc] initWithData:returnData 编码:NSUTF8StringEncoding]; NSLog(@“上传图片:%@”,图片); code> pre> blockquote> div>
答
Change the mime type to match what you're actually sending (image/jpeg
).
Delete one of these duplicate lines:
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
(and probably at least log the error parameter...)