Hi All, Can anyone provide me some links or examples to upload files to the HTTP server using iphone APIs.
Thanks in Advance, BP
Hi All, Can anyone provide me some links or examples to upload files to the HTTP server using iphone APIs.
Thanks in Advance, BP
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);
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"];
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
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
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]
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];