0

朋友们

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

- (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,“msg”:“上传文件时出错,请重试!”}

4

2 回答 2

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'] ;
    }
?>

希望这可以帮助

于 2012-04-06T07:52:10.843 回答
-1

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

于 2012-04-06T09:54:21.503 回答