上传图像到服务器与iOS不工作

我试图上传一个图像到我的PHP服务器,但它不工作。 当我尝试上传文件时,屏幕会冻结(这是预期的 – 我使用sendSynchronousRequest,直到我可以得到这个工作)大约一分钟,然后返回一个空string作为响应。 我一直在这个工作了一天,似乎无法得到它的工作。 此外,图像从未真正上传到服务器(我检查)。 提前致谢!

// Uploading the image -(IBAction)uploadImage:(id)sender{ [self uploadImage:UIImageJPEGRepresentation(productImageView.image, 90) filename:@"image.jpg"]; } - (BOOL)uploadImage:(NSData *)imageData filename:(NSString *)filename{ // This isn't actually my url btw NSString *urlString = @"http://www.mysite.com/uploadImage.php"; // setting up the request object now NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; /* add some header info now we always need a boundary when we post a file also we need to set the content type You might want to generate a random boundary.. this is just the same as my output from wireshark on a valid html post */ NSString *boundary = @"---------------------------14737809831466499882746641449"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; /* now lets create the body of the post */ NSMutableData *body = [NSMutableData data]; [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"rn" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: application/octet-streamrnrn" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:imageData]]; [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; // setting the body of the post to the reqeust [request setHTTPBody:body]; // now lets make the connection to the web NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Responce" message:returnString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; return ([returnString isEqualToString:@"OK"]); } 

和我的PHP文件:

 <?php $uploaddir = ''; // Put in same directory as PHP file for debugging purposes $file = basename($_FILES['userfile']['name']); $uploadfile = $uploaddir . $file; if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { echo "Your file is called {$file}"; } ?> 

麻烦在图像上传时将variables传递给PHP脚本

这将工作肯定。

你错过了CRLF字符的转义,例如

从更改

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

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

完整的修复:

 /* 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]];