2012-04-06 97 views
0

朋友通过我的iPhone上传图片

我正在通过此代码上传图片。

- (void) uploadData:(NSData *)imageData 
{ 
// setting up the URL to post to 
NSString *urlString = @"http://www.abc.com/clients/def/upload.php?fk_abc_id=2"; 

// 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 
*/ 

/* connection.setRequestProperty("Connection", "Keep-Alive"); 

connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 

outStream = new DataOutputStream(connection.getOutputStream()); 

outStream.writeBytes(twoHyphens + boundary + lineEnd); 
outStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"" + imagepath +"; " + lineEnd); 
outStream.writeBytes(lineEnd);*/ 


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=\".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 

NSLog(@"body is :%@",body); 
[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(@"return string is :%@",returnString); 
} 

但我得到响应返回字符串:{ “STAT”:0, “味精”: “有一个上传文件时出错,请重试!”}

+0

谢谢可可事宜。 – PJR 2012-04-06 06:32:09

+1

请比较“Content-Disposition:form-data; name = \”userfile \“那东西在php文件上传文件中是否一样?请检查一下 – parag 2012-04-06 06:39:04

+0

你还没有添加刚才输入的图像名称.jpg为什么是这样 – Leena 2012-04-06 06:45:43

回答

2

尝试使用对于图片上传

UIImage*myImage=[UIImage imageNamed:@"dr.jpg"]; 
imageView.image=myImage; 

NSData *imageData = UIImageJPEGRepresentation(imageView.image, 1.0); 
// setting up the URL to post to 
NSString *urlString = @"http://www.myserver.com/imageupload.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=\"dr.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); 

下面下面的代码是PHP代码

<?php 
     $uploaddir = './'; //Uploading to same directory as PHP file 
     $file = basename($_FILES['userfile']['name']); 
     $uploadFile = $file; 
     $randomNumber = rand(0, 99999); 

     $newName = $uploadDir . $uploadFile; 

    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) { 
    echo "Temp file uploaded. \r\n"; 
    } else { 
    echo "Temp file not uploaded. \r\n"; 
    } 
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $newName)) { 
     $postsize = ini_get('post_max_size'); //Not necessary, I was using these 
     $canupload = ini_get('file_uploads'); //server variables to see what was 
     $tempdir = ini_get('upload_tmp_dir'); //going wrong. 
     $maxsize = ini_get('upload_max_filesize'); 
     echo "http://www.iroboticshowoff.com/dir/{$file}" . "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ; 
    } 
?> 

希望这可以帮助

+0

什么shold在控制台中显示为returnString?我没有任何东西显示在这里... – suMi 2013-11-13 13:53:59

+0

这个常见任务的客户端和服务器代码的很棒的例子! – techsMex 2014-04-27 05:35:10

-1

请参阅我的demo应用程序从iOS上载服务器上的图像/视频。用户可以使用ASIHTTPRequest上传任何类型的文件。这是一个简单的演示。