【问题标题】:iOS Photo Upload to a serveriOS 照片上传到服务器
【发布时间】:2012-10-10 09:48:15
【问题描述】:

我几天来一直在尝试如何将图像上传到 API。那里有一个像这样的 cURL 示例。

curl "http://address.com/api/" -F "parameter[name]=My Upload" -F "parameter[description]=this is my upload" -F "parameter[other]=additional info" -F "parameter[image]=@wake_2560x1600.jpg;type=image/jpg" 

我将如何使用它通过 NSMutableURLRequest 和 NSURLConnection 上传到服务器?我尝试了几种不同的方法,但都没有成功。有人有什么想法吗?

编辑:

我目前正在试用 ASIFormDataRequest

NSData *imageData = UIImageJPEGRepresentation(image, 0.9);
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request addRequestHeader:@"User-Agent" value:@"iOS App"];
[request addPostValue:@"name" forKey:@"parameter[name]"];
[request addPostValue:@"description" forKey:@"parameter[description]"];
[request addPostValue:@"other" forKey:@"parameter[other]"];
[request addData:imageData withFileName:[NSString stringWithFormat:@"jpeg_%d.jpg", rand()] andContentType:@"image/jpeg" forKey:@"param[image]"];
[request startAsynchronous];

【问题讨论】:

    标签: ios image api upload


    【解决方案1】:

    粗略的做法。

    PHP 脚本。

    <?php
    $target_path  = "./";
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
     echo "The file ".  basename( $_FILES['uploadedfile']['name']).
     " has been uploaded";
    } else{
     echo "There was an error uploading the file, please try again!";
    }
    ?>
    

    IOS代码

    - (void)upload:(UIImage *)image {
        NSData *imageData = UIImageJPEGRepresentation(image, 80);
        NSString *urlString = @"http://www.upload.com/upload.php"; // URL of upload script.
    
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];
    
        NSString *boundary = @"---------------------------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:[@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"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]];
        [request setHTTPBody:body];
    
        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
        NSLog(@"returnString: %@", returnString);
    }
    

    只要确保你使用的变量名是相同的,这里我对 php 和 ioscode 都使用“uploadedfile”

    php

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
    

    ios

    [body appendData:[@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    

    // 虽然我现在使用https://github.com/samvermette/SVHTTPRequest

    如果您在代码中导入该库,那么上传就这么简单

    [SVHTTPRequest POST:@"http://www.upload.com/upload.php"
                parameters:[NSDictionary dictionaryWithObjectsAndKeys:imageData, @"uploadedfile", nil]
                  progress:^(float progress) {
                      progressLabel.text = [NSString stringWithFormat:@"Uploading (%.0f%%)", progress*100];
                  }
                completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
                    progressLabel.text = @"Upload complete";
                }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多