【发布时间】: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