views:

154

answers:

3

Hey !

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?

A: 

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

mvds
So I have to write this : NSString *boundary = [NSString stringWithString:@"14737809831466499882746641449"]; ?
Pierre
yes, or even `NSString *boundary = @"14737809831466499882746641449";`
mvds
A: 

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.

Jesse Beder
Sorry, I don't know how to use this command :(
Pierre
A: 

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"];
chilitechno.com
Thanks. I tried but doesn't work :(I wrote :ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlString]]; [request setData:imageData withFileName:@"myImage.png" andContentType:@"image/png" forKey:@"photo"];
Pierre
What's the problem?
chilitechno.com
Nothing happens ...
Pierre
Do you actually send the request?[request startSynchronous];
chilitechno.com
Yes. Problem solved, it was the compagny server which sucks -_-
Pierre