duanmei1930 2012-07-31 14:46
浏览 34

objective-C将png文件上传到http

I'm getting an error uploading a file to a http server. Here is the code that I'm using:

    UIImage * ourImage = [UIImage imageNamed:@"tux.png"];
    NSData * imageData = UIImageJPEGRepresentation(ourImage, 1.0);  

    NSString *urlString = @"http://www.site.com/imageupload.php";      
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSMutableData *body = [NSMutableData data];


    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];



    [body appendData:[[NSString stringWithFormat:@"--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Disposition: attachment; name=\"userfile\"; filename=\".png\"
"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream

"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithString:@"
"] dataUsingEncoding:NSUTF8StringEncoding]];

    // Text parameter1
    NSString *param1 = @"parameter text";
    [body appendData:[[NSString stringWithFormat:@"--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter1\"

"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:param1] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"
"] dataUsingEncoding:NSUTF8StringEncoding]];

    // Another text parameter
    NSString *param2 = @"Parameter 2 text";
    [body appendData:[[NSString stringWithFormat:@"--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter2\"

"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:param2] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"
"] dataUsingEncoding:NSUTF8StringEncoding]];

    // close form
    [body appendData:[[NSString stringWithFormat:@"--%@--
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // set request body
    [request setHTTPBody:body];

    //return and test
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"%@", returnString);    

and this is the PHP file that I have in public_html/ directory in the Server. The file name is imageupload.php. I am trying to upload an Image file via HTTP not using any log in name or password. I am not to familiar with PHP and really need some help. Thanks.

    <?php
     $uploaddir = '/public_html/upload';
     $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. 
";
    } else {
    echo "Temp file not uploaded. 
";
   }

   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}" . "
" .  $_FILES['userfile']
         ['size'] . "
" . $_FILES['userfile']['type'] ;
    }
    ?>

and this is the error that I'm getting back from the server when I execute the code:

2012-07-31 10:04:24.576 FTP6[3600:f803]
PHP Error Message
Parse error: syntax error, unexpected T_STRING in /home/a5054180/public_html/imageupload.php on line 2

Free Web Hosting

Plus other errors. Can anyone try it out and let me know what I can do to fix it. I would appreciate it thanks a lot.

  • 写回答

1条回答 默认 最新

  • duanlei1957 2013-01-11 17:58
    关注

    I haven't tested your script, but I think you should change your Content-Disposition and Content-Type to:

    Content-Disposition: form-data; name="userfile"; filename="front.png" Content-Type: image/png

    Do you know the program Charles? It's amazingly helpful. It's a proxy and helps you to analyze your request and what the server response is.

    http://www.charlesproxy.com/

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀