我正在尝试将图片上传到我的php服务器,但它无法正常工作。当我尝试上传文件时,屏幕只是冻结(这是预期的 - 我正在使用sendSynchronousRequest,直到我可以让它工作)大约一分钟,然后它返回一个空字符串作为响应。我已经在这方面工作了一天多,似乎无法让它发挥作用。此外,图像从未实际上传到服务器(我检查过)。提前谢谢!
// 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}";
}
?>
答案 0 :(得分:2)
您错过了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]];
答案 1 :(得分:1)