如何将图像上传到iPhone的远程服务器? [英] How to upload image to remote server in iphone?

查看:115
本文介绍了如何将图像上传到iPhone的远程服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试上传一张我在相机帮助下点击的图片。我正在尝试以下代码将图像上传到远程服务器。

I am trying to upload a image which i am clicking with the help of the camera. I am trying the following code to upload the image to the remote server.

-(void)searchAction:(UIImage*)theImage
{
    UIDevice *dev = [UIDevice currentDevice];
    NSString *uniqueId = dev.uniqueIdentifier;

    NSData * imageData = UIImagePNGRepresentation(theImage);

    NSString *postLength = [NSString stringWithFormat:@"%d",[imageData length]];

    NSString *urlString = [@"http://www.amolconsultants.com/im.jsp?" stringByAppendingString:@"imagedata=iPhoneV0&mcid="];
    urlString = [urlString stringByAppendingString:uniqueId];
    urlString = [urlString stringByAppendingString:@"&lang=en_US.UTF-8"];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:imageData];

    NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (conn == nil) 
    {
        NSLog(@"Failed to create the connection");
    }
}

但没有任何内容发布。控制台窗口中也没有任何内容。我在动作表中调用此方法。当用户点击操作表的第一个按钮时,会调用此方法发布图像。

But nothing is getting posted. Nothing comes in the console window also. I am calling this method in the action sheet. When the user clicks on the 1st button of the action sheet this method is called to post the image.

任何人都可以帮我这个...

Can anyone help me with this...

任何代码都会非常有用......

Any code will be very helpful...

提前完成Thanx ...

Thanx in advance...

推荐答案

您可以使用第三方的库 asi-http-request 。它简化了大部分的努力工作。在您的情况下,您只需执行以下操作:

You can use the libraries from a third party called asi-http-request. It simplyfies for you most of the hard works. In your case, you just do as the following:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:imageData];
[request setRequestMethod:@"PUT"];

这篇关于如何将图像上传到iPhone的远程服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆