将图像从 iPhone 发送到服务器并使用 restful 将其存储在服务器上 [英] send image to the server from iPhone and store it on the server using restful

查看:60
本文介绍了将图像从 iPhone 发送到服务器并使用 restful 将其存储在服务器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 iPhone 应用程序,用户应该在使用前订阅它.订阅时他应该添加他从画廊的照片,我的问题是如何通过POST方法将该图片发送到服务器端,我使用的是restful webservices,从服务器端恢复后如何将其存储在服务器上.

I create an iPhone application where the use should subscribe before using it. While subscribing he should add his picture from the galerie, My problem is how to send that picture to the server side via a POST methode, I'm using restful webservices, and after recuperate it from the server side how can I store it on the server.

任何帮助将不胜感激非常感谢您提前.

Any help would be very appreciated Thank you a lot in advence.

推荐答案

使用 NSMutableURLRequest 你可以上传图片到服务器

Using NSMutableURLRequest you can upload image to server

-(void)uploadImage
{
    NSString *strURL =@"your server url";// Url server
    UIImage *img = [UIImage imageNamed:@"image.png"];
    NSData *file1Data = UIImagePNGRepresentation(img);
    NSString *name=[NSString stringWithFormat:@"image.png"];
    // NSData *file1Data = [NSData dataWithContentsOfURL:recordedTmpFile];

    //Create web Service
    // NSString *urlString = @"http://demos.nanostuffs.com/iphone/audio.php";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:[NSURL URLWithString:strURL]];
    [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:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",name]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:file1Data]];
    [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] ;
}

这篇关于将图像从 iPhone 发送到服务器并使用 restful 将其存储在服务器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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