【问题标题】:How to send images to a web service?如何将图像发送到 Web 服务?
【发布时间】:2010-09-07 07:32:27
【问题描述】:

嘿,任何人都可以发布一个示例代码,演示如何将图像(jpeg、png)发布到本地网络服务上。

赛义德

【问题讨论】:

    标签: iphone iphone-sdk-3.0


    【解决方案1】:

    这是一个代码sn-p -

    - (void)useImage:(UIImage*)theImage
    {
        /*
         turning the image into a NSData object
         getting the image back out of the UIImageView
         setting the quality to 90
         */
    
        NSData *imageData = UIImageJPEGRepresentation(theImage, 90);
        // setting up the URL to post to
    
    
        NSString *urlString=[SiteUrl    stringByAppendingString:@"iphone_upload_photo.php?type=colleague&token="];
        urlString=[urlString stringByAppendingString:PublicToken];
    
    
    
    
    
        // 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
    
         */
        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=\"ipodfile.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];
    
    
    
    
    
    }
    

    编辑:代码解释 -

    我们在这里所做的是,我们只是将图像提交到服务器端页面,就像我们在 HTML 页面中所做的那样假设我们有一个这样的 html 页面 -

    <form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    Choose a file to upload: <input name="uploadedfile" type="file" /><br />
    <input type="submit" value="Upload File" />
    </form>
    

    我们可以像这里一样在 PHP 代码中接收这个文件 -

    $target_path = "uploads/";
    
    $target_path = $target_path . basename( $_FILES['userfile']['name']); 
    
    // $_FILES['userfile']['name'] will be "ipodfile.jpg"
    
    if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['userfile']['name']). 
        " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
    

    所以我们在目标代码中所做的是我们只是创建一个 POST 请求并为这个我们发送文件的 post 请求设置 http 标头,文件字段名称是“userfile”,确切的文件名是“ipodfile .jpg"

    你可以直接使用php代码来测试这个东西。

    希望它能消除混乱。

    【讨论】:

    • 嘿 saurabh,这是什么行:[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg \"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    • 在这一行中,我们设置 Http 标头“Content-Disposition”,设置输入文件字段名称和文件名称(您将在服务器上收到)。如果您需要更多详细信息,请告诉我。我有空 4-5 小时。
    • 是的,老板,我不知道它是如何实现的。我们传递的这个 NSData 应该如何在另一端(Web 服务)接收并解码以获得原始图像。早些时候,我能够将小图像(最大 4kb)转换为 nsstring base64 编码并将它们发送到 web 服务并在那里解压以获取图像。但现在的要求是对各种尺寸的图像都这样做。你能带我看一下代码吗?例如-我有一个名为“van gogh.png”的图像,现在我应该做什么?
    【解决方案2】:

    首先google hit 可能会对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-21
      相关资源
      最近更新 更多