2
我在從我的iOS應用程序上傳照片到我的網絡服務器時出錯。將照片從iOS上傳到服務器時出錯
我想知道是否有人知道問題出在哪裏,以及如何解決問題。任何幫助,將不勝感激。
以下是錯誤消息:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator at
[email protected] to inform them of the time this error occurred,
and the actions you performed just before this error.</p>
<p>More information about this error may be available
in the server error log.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>
這裏是iOS的代碼:
- (IBAction)uploadPhoto:(id)sender {
NSData *imageData = UIImageJPEGRepresentation(_imageView.image, 90);
NSString *urlString = @"http://website.ca/uploadPhoto.php";
NSMutableURLRequest *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:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".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]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
}
這裏是PHP代碼:
<?php
$uploaddir = 'uploads/';
$ran = rand() ;
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir .$ran.$file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "Uploaded";
}
?>
做一個print_r($ _ FILES);並在此添加。 – eddwinpaz
什麼錯誤給你? – eddwinpaz
我應該推薦使用AFNetworking它更簡單的包裝來做這個東西..因爲你的PHP代碼似乎是正確的。在發佈二進制數據傳輸時,代碼問題應該在您的Objective-C代碼上 – eddwinpaz