iphone - 通过我的 iPhone 上传图像

标签 iphone objective-c ios image image-uploading

friend

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

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

最佳答案

尝试使用以下代码进行图像上传

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

希望这有帮助

关于iphone - 通过我的 iPhone 上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10039930/

相关文章:

iphone - didFinishPickingMediaWithInfo 返回零照片

iphone - CATextLayer 在 iOS7 中的视频中不可见

iphone - 如何在 "search"应用程序中使用 UItableView 和 UINavigationController ?最佳实践

iphone - 如何在 iPhone 中创建短信应用程序

iphone 开发 : how to auto facebook login if the facebook app already logged in

ios - 支持 iPhone 纵向和 iPad 横向+纵向的通用应用程序

iphone - iTunes App 被拒绝

iphone - 过滤 iPhone 地址簿中的联系人

iphone - "Data Formatters temporarily unavailable..."错误

objective-c - 我可以在 iOS 上的 C/Objective-C 中创建内存映射 FILE * 吗?