我目前正在为iPhone应用程序上传图像功能
以下代码适用于localhost
- (void) imageUpload:(UIImage *) image{
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
NSString *urlString = @"http://xxxxxxxxxx/uploadtest/upload.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 setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"image.png\"\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];
//start uploading
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//get return string
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
}
然而,当我为我的服务器尝试此代码时,它将返回500内部服务器错误...
调试后,我资助,当我从请求的http标题中删除@"multipart/form-data;"
时,不会出现错误
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
但是如果没有"multipart/form-data"
我想知道服务器中是否有任何设置禁用文件上传限制或我的代码问题... 如果有人有解决方案,请帮助我 感谢〜