【问题标题】:Upload image from iOS app to web server using php showing error?使用 php 将图像从 iOS 应用程序上传到 Web 服务器显示错误?
【发布时间】:2015-09-18 10:27:21
【问题描述】:

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

当我尝试将图像上传到亚马逊网络服务器时出现此错误

Image Return String: file=test.pngerror

我的代码

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 脚本

 <?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 中,我进行了此更改以避免安全错误 这些是我在 plist 中所做的更改

<key>NSAppTransportSecurity</key>  
 <dict>  
      <key>NSAllowsArbitraryLoads</key><true/>  
 </dict>

请帮我解决这个问题

【问题讨论】:

  • 我想发送一个名为 test.png name= \"test\" and filename = \".png\"的文件
  • 我认为在此链接中您可以找到解决方案,因为 iphone 代码中可能有错误,因为 php 代码与此链接中的相同 stackoverflow.com/questions/23566897/…

标签: php ios objective-c uiimage


【解决方案1】:

您似乎手动将图像格式更改为 .png。请尝试以原始格式上传图片。

【讨论】:

  • 这是评论而非答案!
  • 默认所有图片在ios中都是.png
  • @testuser 我没明白你的意思。你不是自己在项目中添加了测试图像吗?你从哪里得到 test.png?
  • 请阅读我的问题,在第一行中我提到“我拍摄的图像”是指我通过相机拍摄的。
  • 我认为 php 创建问题
猜你喜欢
  • 2017-10-26
  • 1970-01-01
  • 1970-01-01
  • 2013-06-22
  • 2013-09-07
  • 2016-01-02
  • 2020-12-25
  • 1970-01-01
  • 2013-12-20
相关资源
最近更新 更多