【问题标题】:uploading image to server from iphone从iphone上传图片到服务器
【发布时间】:2012-02-19 01:56:26
【问题描述】:

经过研究,我想出了如何像这样将图像上传到我的服务器

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:

$newname = "image01.jpg";
$place_file = move_uploaded_file( $_FILES['userfile']['tmp_name'], "members/$id/".$newname) or die("FAILED to Upload");

但是,如果我不需要,我不想使用 NSMutableURLRequest,应用程序中的所有其他内容我一直在使用 ASIFormDataRequest,我也想用它来上传图像

这是我尝试过的方法,但失败了:

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];

提前致谢!!

【问题讨论】:

  • “但是失败了”——到底发生了什么?
  • 它不上传图片。 php发回“上传失败”

标签: php ios image upload


【解决方案1】:

【讨论】:

  • 旁注:ASI 已停产。
  • 非常正确,AFNetworking 是一个更好的替代品。
猜你喜欢
  • 1970-01-01
  • 2012-06-23
  • 2014-06-08
  • 1970-01-01
  • 1970-01-01
  • 2012-05-02
  • 1970-01-01
  • 2011-06-01
相关资源
最近更新 更多