首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用php将图片从iOS应用程序上传到web服务器显示错误?

问使用php将图片从iOS应用程序上传到web服务器显示错误?
EN

Stack Overflow用户
提问于 2015-09-18 18:27:22
回答 1查看 145关注 0票数 2

我捕获了图像并将其显示在UIImageView中。

当我尝试将图片上传到Amazon web服务器时,我收到此错误

代码语言:javascript
复制
Image Return String: file=test.pngerror

我的代码

代码语言:javascript
复制
NSData *imageData = UIImagePNGRepresentation(cell4.img.image);
NSString *urlString = @"http://myipaddress/allapps/php/test.php";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------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:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"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([NSString stringWithFormat:@"Image Return String: %@", returnString]);

和我的php脚本

代码语言:javascript
复制
 <?php
$uploaddir = 'uploads/';
$file = basename($_FILES['uploadedfile']['name']);
$uploadfile = $uploaddir . $file;

echo "file=".$file; //is empty, but shouldn't

if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploadfile)) {
    echo $file;
}
else {
    echo "error";
}
?>

在我的info.plist中,为了避免安全错误,我做了这样的更改

代码语言:javascript
复制
<key>NSAppTransportSecurity</key>  
 <dict>  
      <key>NSAllowsArbitraryLoads</key><true/>  
 </dict>

请帮我解决这个问题

EN

回答 1

Stack Overflow用户

发布于 2015-09-18 18:30:43

看起来你是手动将图片格式改成了.png。请尝试上传原始格式的图片。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32649727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档