尝试将图像发布到服务器时发生iOS - 500错误

时间:2014-06-13 04:20:11

标签: php objective-c ios7

我目前正在为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"

,我无法从我的php文件中获取任何文件数据

我想知道服务器中是否有任何设置禁用文件上传限制或我的代码问题... 如果有人有解决方案,请帮助我 感谢〜

0 个答案:

没有答案