views:

13714

answers:

7

Hi All, Can anyone provide me some links or examples to upload files to the HTTP server using iphone APIs.

Thanks in Advance, BP

+10  A: 

The code below uses HTTP POST to post NSData to a webserver. You also need minor knowledge of PHP.

NSString *urlString = @"http://yourserver.com/upload.php";
NSString *filename = @"filename";
request= [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:YOUR_NSDATA_HERE]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);
Brandon Schlenker
Brandon, Thanks for your responsein the above code i have some questions,1) Where we are passing upload file path 2) what i need tp pass here YOUR_NSDATA_HERE actually i am having file name called test.txt in this path /Users/abc/Desktop/test.txtcan you tell me where should i pass this info using above code ,i executed above code it gives NSInvalidArgumentException.please help pleas..--BP
BP
You need to convert your text file to NSData.NSData *data = [[NSData alloc] initWithContentsOfFile:path];"path" is obviously the path to your text file such asNSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"image.jpg"];
Brandon Schlenker
Thanks Brandon, i have one question , i have 1 browse button if i choose it should open iphone or ipod files all txt or imgs do we have any kind of API's to open file system . and that files pth i need to add to NSData. i am sorry if i am wrong but please help me please.Thanks in advance.
BP
There isnt any sort of file picker. I dont think you can access files outside of files that your app has created.
Brandon Schlenker
I tried this out on my own and I keep getting the errors from my webserver about the format of the post message (System.InvalidOperationException: Request format is invalid: multipart/form-data; boundary=---------------------------14737809831466499882746641449) . Does anyone get errors?
Abel Martin
Can u plz modify it to send multiple files at a time ?
Biranchi
This looks nice. I don't see any PHP though. I think you meant HTTP.
MattDiPasquale
+11  A: 

ASIHTTPRequest is a great wrapper around the network APIs and makes it very easy to upload a file. Here's their example (but you can do this on the iPhone too - we save images to "disk" and later upload them.

ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:@"Ben" forKey:@"first_name"];
[request setPostValue:@"Copsey" forKey:@"last_name"];
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];
Jane Sales
Thanks man, this was exactly what I was looking for!I was so amazed it was so hard to find something like this.
quano
A: 

Hi Friends,

So we can go with ASIFormDataRequest instead of posting NSData to the url.how are we going to send above request ? and how are we going to recive at server side servlet/php .are we going to get multipart form data and we get it or do we have a unique id for a file uploaded in a form as we do in html like input type=file name="myfile" /> . do we have any resources file attached to this ? can you please eloborate a bit more and give me resources (source files) if any..

Thanks & Regards, sajid

Sonu
A: 

I have made a lightweight backup method for the Mobile-AppSales app available at github

I wrote about it here http://memention.com/blog/2009/11/22/Lightweight-backup.html

Look for the - (void)startUpload method in ReportManager.m

epatel
A: 

hi

in case i want to upload the filename along with the text like username ,caption & tag for the image .. can any one post it for this .else mail me to [email protected]

siva
+2  A: 

This is a great wrapper, but when posting to a asp.net web page, two additional post values need to be set:

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    //ADD THESE, BECAUSE ASP.NET is Expecting them for validation
    //Even if they are empty you will be able to post the file
    [request setPostValue:@"" forKey:@"__VIEWSTATE"];
    [request setPostValue:@"" forKey:@"__EVENTVALIDATION"]; 
    ///

    [request setFile:FIleName forKey:@"fileupload_control_Name"];
    [request startSynchronous];
A: 

hello

If someone wants to has in his form and some other text fields, how can he handle that?

thank u in advance,

harris

jova
please post a new question, rather than adding an answer to an existing question
JosephH