iPhone : upload image to web aspx file

别说谁变了你拦得住时间么 提交于 2019-12-11 20:13:22

问题


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?


回答1:


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"];



回答2:


normally, the boundary does not contain the --- when it's specified in the Content-Type. Apart from that, can't see anything unusual.




回答3:


Try to upload it to the aspx server from the command line with curl. Once you've got that working, pass -v (verbose) or -i (include HTTP headers) to check exactly what is being sent, and then you can try to replicate that in Cocoa.



来源:https://stackoverflow.com/questions/3366260/iphone-upload-image-to-web-aspx-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!