[英]uploading image to server from iphone
After research I have figured out how to upload an image to my server like so 经过研究,我想出了如何像这样将图像上传到服务器
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"smiley1.jpg"], 90);
NSString *urlString = @"http://www.mysite.com/edit_profile.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[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 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]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
php: 的PHP:
$newname = "image01.jpg";
$place_file = move_uploaded_file( $_FILES['userfile']['tmp_name'], "members/$id/".$newname) or die("FAILED to Upload");
However, I don't want to use NSMutableURLRequest if I don't have to, with everything else in the app I have been using ASIFormDataRequest and I want to also use it to upload the image 但是,如果不需要,我不想使用NSMutableURLRequest,而我在应用程序中的其他所有内容我都在使用ASIFormDataRequest,并且我也想使用它来上传图像
Here is how I have tried, but failed: 这是我尝试过但失败的方法:
NSURL *url = [NSURL URLWithString:@"http://www.mysite.com/edit_profile.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"smiley1.jpg"], 90);
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]];
[request setPostValue:contentType forKey:@"Content-Type"];
[request setPostBody:body];
[request setDelegate:self];
[request startAsynchronous];
Thanks in advance!! 提前致谢!!
ASIFormDataRequest
具有处理文件上传的内置方式 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.