【问题标题】:how to post an image to the web server如何将图像发布到 Web 服务器
【发布时间】:2012-12-03 07:38:15
【问题描述】:

我正在使用 json 解析处理 Web 服务。我能够从网络服务中获取图像,有人可以帮助我如何发布图像吗?如何将图像发布到网络服务?

【问题讨论】:

  • 您考虑过使用ASIHTTPRequest 吗?
  • @mstfbsnli 我是 iOS 新手......所以,请帮助我,我如何通过 ASIHTTPRequest 实现它......请......
  • 这里是explanation的用法

标签: iphone objective-c ios xcode


【解决方案1】:

这将是类似的事情......

NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:@"you url"];

[mutableRequest addValue:@"image/jpeg" forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];

[body appendData:[NSData dataWithData:UIImageJPEGRepresentation(self.image, 0.9)]];

[mutableRequest setHTTPBody:body];

NSURLResponse *response;
NSError *error;

(void) [NSURLConnection sendSynchronousRequest:mutableRequest returningResponse:&response error:&error];

谢谢

【讨论】:

  • 谢谢..但是我必须以哪种方法添加这些代码行。
  • 不知道你的意思是哪个方法?这完全取决于您如何编写程序。在某些时候,您将拥有一个名为“uploadImage”或其他东西的方法。这就是你上传图片的方式。
  • tnx .. 我问你这样的问题是因为我是 iOS 新手。所以,对它没有太多想法..
【解决方案2】:

在此处查找使用随机名称上传图片的服务器端 (PHP) 编码。它也会给出图片链接作为响应。

//Create a folder named images in your server where you want to upload the image.
// And Create a PHP file and use below code .


<?php
$uploaddir = 'images/';
$ran = rand () ;

$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir .$ran.$file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "www.host.com/.../images/{$uploadfile}";
}
?>

这是 iOS 代码

- (IBAction)uploadClicked:(id)sender
{
    /*
         turning the image into a NSData object
         getting the image back out of the UIImageView
         setting the quality to 90
        */
        NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);
        // setting up the URL to post to
        NSString *urlString = @"your URL link";

        // 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
        */
        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:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
        [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="userfile"; filename="ipodfile.jpg"rn"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",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);

}

【讨论】:

  • 我是iOS新手。所以,你能详细说明一下如何做到这一点吗??
  • 查看我编辑的答案,让我知道您面临的问题或您无法理解的行。
  • 在我的项目中,我的服务器端不是 php 它的 python.so,需要进行哪些更改??
  • tnx..很多..@siba Prasad Hota
  • @SibaPrasadHota你能告诉我是否需要一次在 php Web 服务器上发送大量图像。那么应该是什么好方法呢。我已将图像数据大小减少到 100 kb。谢谢
【解决方案3】:

查看下面的好教程。在该教程中,您也可以找到 ios 端代码以及服务器端 php 代码。 http://zcentric.com/2008/08/29/post-a-uiimage-to-the-web/

【讨论】:

  • tnx..但是有必要知道服务器端..因为我只有post..no的url,服务器端信息..
  • 只要问服务器端开发人员他实现了什么,还要求他提供一个 html 表单来验证他的代码。他身边可能有问题,这可能会导致你做噩梦:)
  • 您提到的链接已经很好地描述了它,但发布的图片在那里不起作用..您还有其他链接或代码..请帮助..
  • 你必须用你的服务器链接测试它
【解决方案4】:

使用ASIHttpFormData的最佳方式Click here to know how to use

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
  • 1970-01-01
  • 2018-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-13
相关资源
最近更新 更多