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 に答える