我正在学习这个人的教程,学习如何将图像从Xcode上传到我电脑上的本地文件夹。
当我编译并运行代码时,一切都很好,没有错误,但图像不在应该放的文件夹中。
程序应如何工作的简要总结:
-
用户使用UIImagePicker 从照片库中选择图像
-
UIImageview将更新为用户已选择的UIImage
-
用户按下"上传"按钮,该按钮将IBAction称为"上传图像"
-
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/中我的文件夹中没有图像
有人知道我做错了什么吗?我觉得我在某个地方犯了一个新手的错误。
提前感谢!
下面是一个例子:
#define DataDownloaderRunMode @"myapp.run_mode"
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60];
[request setHTTPMethod:@"POST"];
// We need to add a header field named Content-Type with a value that tells that it's a form and also add a boundary.
// I just picked a boundary by using one from a previous trace, you can just copy/paste from the traces.
NSString *boundary = @"----WebKitFormBoundarycC4YiaUFwM44F6rT";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
// end of what we've added to the header
// the body of the post
NSMutableData *body = [NSMutableData data];
// Now we need to append the different data 'segments'. We first start by adding the boundary.
[body appendData:[[NSString stringWithFormat:@"'r'n--%@'r'n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Now append the image
// Note that the name of the form field is exactly the same as in the trace ('attachment[file]' in my case)!
// You can choose whatever filename you want.
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name='"attachment[file]'";
filename='"picture.png'"'r'n"] dataUsingEncoding:NSUTF8StringEncoding]];
// We now need to tell the receiver what content type we have
// In my case it's a png image. If you have a jpg, set it to 'image/jpg'
[body appendData:[[NSString stringWithString:@"Content-Type: image/png'r'n'r'n"] dataUsingEncoding:NSUTF8StringEncoding]];
// Now we append the actual image data
[body appendData:[NSData dataWithData:imageData]];
// and again the delimiting boundary
[body appendData:[[NSString stringWithFormat:@"'r'n--%@--'r'n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// adding the body we've created to the request
[request setHTTPBody:body];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self
startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:DataDownloaderRunMode];
[connection start];