【发布时间】:2015-01-06 02:48:39
【问题描述】:
我正在关注此人的tutorial,了解如何将图像从 Xcode 上传到我计算机上的本地文件夹。
当我编译并运行代码时,一切都很好,我没有收到任何错误,但图像不在它应该在的文件夹中。
程序应该如何工作的简要总结:
用户使用 UIImagePicker 从他们的照片库中选择一张图片
UIImageview 使用用户选择的 UIImage 进行更新
用户按下调用 IBAction“uploadImage”的“上传”按钮
uploadImage 函数会将 UIImage 转换为 NSData 并将其发送到 PHP
- PHP 会将图片上传到所选文件夹
IBAction“uploadImage”的代码
-(IBAction)uploadImage{
//this is the url where the PHP file is located
NSString *urlString = @"http://localhost/~andy/uploadimage.php";
//Convert selected UIImage to NSData
NSData *imageData = UIImageJPEGRepresentation(chosenImage,.3);
if (imageData != nil)
{
NSString *filenames = [NSString stringWithFormat:@"imageUploaded"];
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 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=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//I use a method called randomStringWithLength: to create a unique name for the file, so when all the aapps are sending files to the server, none of them will have the same name:
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n",[self randomStringWithLength:10]] 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];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//Run code async
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSData *returnData = data;
NSLog(@"data recieved!%@", returnData);
//Do what you want with your return data.
}];
}
}
这是uploadimage.php的php代码:
<?php
$target_path = "/"; //where I want the uploaded images to go
$target_path = $target_path . basename( $_FILES['userfile']['name'] );
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) {
echo "Image uploaded";
}
else {
echo "Upload Failed";
}
?>
我想要存储图片的目录是本地本地目录uploadimagep.php所在的localhost/~andy/
运行程序后的结果
一切都返回成功,但我的 localhost/~andy/ 文件夹中没有图像
有谁知道我做错了什么?我觉得我在某个地方犯了一个菜鸟错误。
提前致谢!
【问题讨论】:
标签: php xcode upload nsurlconnection