iPhone:将图片上传到网页aspx文件 [英] iPhone : upload image to web aspx file

查看:212
本文介绍了iPhone:将图片上传到网页aspx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题。
我必须使用POST方法将iPhone上的照片上传到网络服务器,但服务器文件在aspx中。
我用我的服务器和PHP文件尝试了我的代码:效果很好!
现在使用aspx文件:不会上传:(b / b)

我没有对.aspx的访问权。



这里是我的iphone代码:

  NSData * imageData = UIImageJPEGRepresentation(imageView.image,70); 

NSString * urlString = @http://iphone.domain.net/upload_photos.aspx;


NSMutableURLRequest * request = [[[[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@POST];

NSString * boundary = [NSString stringWithString :@--------------------------- 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\\\
,boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body ap pendData:[[NSString stringWithFormat:@Content-Disposition:form-data;名称= \ photo\;文件名= \%@。jpg \\r\\\
,[c nom]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@Content-Type:应用/ octet-stream \r\\\
\r\\\
] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[ [NSString stringWithFormat:@\r\\\
- %@ - \r\\\
,boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

我认为问题来自Content-Type或我的dataUsingEncoding:参数
你有没有想法解决方案如果你不反对使用第三方库,可以考虑看看ASIHttpRequest。你的生活变得如此简单:



http:// allseeing-i.com/ASIHTTPRequest/



http://allseeing-i.com/ASIHTTPRequest/How-to-use

  ASIFormDataRequest * request = [ASIFormDataRequest requestWithURL:url]; 

//在磁盘上传文件
[请求setFile:@/ Users / ben / Desktop / ben.jpgwithFileName:@myphoto.jpgandContentType:@image / jpeg
forKey:@photo];

//上传一个NSData实例
[请求setData:imageData withFileName:@myphoto.jpgandContentType:@image / jpegforKey:@photo];


I have a little problem. I have to upload a photo from my iPhone to a web server with POST Method but the server file is in aspx. I tried my code with my server and PHP file : works well ! Now with aspx file : doesn't upload :(

I don't havec access to the .aspx .

Here is my iphone code :

NSData *imageData = UIImageJPEGRepresentation(imageView.image,70);

NSString *urlString = @"http://iphone.domain.net/upload_photos.aspx";


NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------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",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"%@.jpg\"\r\n",[c nom]] 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]];
[request setHTTPBody:body];

I think the problem comes from the Content-Type or for my dataUsingEncoding: parameter. Have you got and idea to solve it?

解决方案

If you're not opposed to using a 3rd party library consider looking at ASIHttpRequest. It'll make your life so much easier:

http://allseeing-i.com/ASIHTTPRequest/

http://allseeing-i.com/ASIHTTPRequest/How-to-use

  ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

    // Upload a file on disk
    [request setFile:@"/Users/ben/Desktop/ben.jpg" withFileName:@"myphoto.jpg" andContentType:@"image/jpeg"
    forKey:@"photo"];

    // Upload an NSData instance
    [request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];

这篇关于iPhone:将图片上传到网页aspx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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