从iphone上传图片到服务器文件夹
问题描述:
我发现从在线上的图片从iPhone上传图片到服务器文件夹,其显示使用服务器端脚本例如。使用PHP在服务器端从iphone上传图片到服务器文件夹
<?php
$uploaddir = 'uploads/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "Uploaded!";
}else{
echo "Not Uploaded!";
}
/>
是任何possibilites直接上传图片到服务器上的文件夹没有上面的代码,
让的说我要上传到http://111.22.333.44/mysite/pics/
瓦特/ O任何服务器端脚本,如果如何可以做到这一点
感谢
答
/*
turning the image into a NSData object
getting the image back out of the UIImageView
setting the quality to 90
*/
NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
// setting up the URL to post to
NSString *urlString = @"http://iphone.zcentric.com/test-upload.php";
// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
/*
add some header info now
we always need a boundary when we post a file
also we need to set the content type
You might want to generate a random boundary.. this is just the same
as my output from wireshark on a valid html post
*/
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\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]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);
这个代码将会对你有所帮助....
答
如果你设置你的网站服务器允许PUT方法可以做到这一点。 99%的Web服务器只允许GET和POST方法。
从来没有这样做,我不能给你很多帮助。这里是一个网页,我从谷歌得到了谈论它:
我不得不删除与应用行/ octet-让它与attachment_fu一起工作。任何想法为什么? – 2011-04-23 14:09:05
这是伟大的代码。第一次为我工作。谢谢parvind :) – Prerna 2011-07-28 11:50:11
请注意上传文件的大小。你正在创建内存中整个文件内容的请求,然后发送它......如果文件太大(或者你并发地上传了很多文件),你可能会遇到内存消耗问题 – JakubKnejzlik 2014-07-23 03:08:40