I have an audio file that was recorded on the iPhone and I would like to POST it to a website for further processing/storage. How would this be done using Obj-C? If it were .NET or PHP or Python I would just base64 encode the file, upload, and base64 decode server-side. I can't seem to be able to successfully send a file over the wire. Any ideas?
I have to do the same thing only with images. i'm running an asp.net app on the server side which needs to accept a posted image from the iphone.
anyone have a direction on this?
Thanks
John
__________________
---------------------------------------------------------------------- I love being a dad, flying airplanes and writing code.
----------------------------------------------------------------------
Follow me on Twitter: @BostonMerlin
Feed your brain on Twitter: @iPhoneDev101
----------------------------------------------------------------------
iPhone Apps:
Hi guys,
I'm also working on the same scenario, i,e posting audio/video/image file to the web server.
Could anyone please provide some hint regarding the same.
Sample application code will definetly help.
Could you please help me in this regard.
I am pretty sure you can do it without base64 encoding. I am going to be using NSData and then just putting the whole post into a NSString and sending that just like you would see if you sniffed the line to see the transaction.. I will post updates on my failure or success
But really if you say you want something.. please show code and contribute and not just say you did it.
__________________ Iphone Noob iPhone Development Blog. Learn to develop for the iPhone SDK with me
NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
// setting up the URL to post to
NSString *urlString = @"http://iphone.zcentric.com/test-upload.php";
// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
/*
add some header info now
we always need a boundary when we post a file
also we need to set the content type
You might want to generate a random boundary.. this is just the same
as my output from wireshark on a valid html post
*/
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] 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]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
__________________ Iphone Noob iPhone Development Blog. Learn to develop for the iPhone SDK with me
It appears i have the client side code working so my phone is posting an image off to my server running a small asp.net 2.0 webform. I'm using iphone noobs example from his tutorial here (Post a UIImage to the web | Iphone Noob).
My .net webform is setup to receive the file but is not working. i've tried different techniques to get at the file coming into the stream but none work. I'm positive it's because i dont understand the image format being pushed from the phone to the server and how to decode that format on my server.
on my server i have just a few lines of code to accept that stream...
Code:
Dim imageConverter As New System.Drawing.ImageConverter
Dim image As Image = imageConverter.ConvertFrom(Page.Request.InputStream)
image.Save("c:\images\test.png")
the response being returned back from my server to the phone is 'imageconverter cannot convert from system.web.httpinputstream.
Thoughts from those .NET gurus in the crowd?
thanks
john
__________________
---------------------------------------------------------------------- I love being a dad, flying airplanes and writing code.
----------------------------------------------------------------------
Follow me on Twitter: @BostonMerlin
Feed your brain on Twitter: @iPhoneDev101
----------------------------------------------------------------------
iPhone Apps:
StreamReader sr = new StreamReader(Page.Request.InputStream);
string file = Encoding.ASCII.GetString(Convert.FromBase64String(sr.ReadToEnd()));
__________________
---------------------------------------------------------------------- I love being a dad, flying airplanes and writing code.
----------------------------------------------------------------------
Follow me on Twitter: @BostonMerlin
Feed your brain on Twitter: @iPhoneDev101
----------------------------------------------------------------------
iPhone Apps:
got it. using iphone noobs example i had to remove several lines of code from his body string to get this to work. looking at noobs code.. make the following changes:
Code:
NSMutableData *body = [NSMutableData data];
//[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] 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]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
on the server side (for those of you who might be using asp.net).. all that's needed is:
Code:
Dim sr as New StreamReader(Page.Request.InputStream)
Page.Reqeust.SaveAs("c:\my.png", False)
This was one of the last big hurdles for my app.. which i've had many such hurdles. I'm now utilizing threads, webservices to .net servers both pushing data back and forth, posting images to the server and retrieving images from the server. Save those images on the phone, working with myssql, dynamically upating webviews with content and images, tracking javascript events in those views etc. The list seems to go on and on. I do things like this frequently at my day job as a windows dev.. but I'm finally getting my head wrapped around mac development.. and it feels good!
Thanks again for everyones help.
John
__________________
---------------------------------------------------------------------- I love being a dad, flying airplanes and writing code.
----------------------------------------------------------------------
Follow me on Twitter: @BostonMerlin
Feed your brain on Twitter: @iPhoneDev101
----------------------------------------------------------------------
iPhone Apps:
I am using the same thing to upload the xml but doesn't seems to be work. My webform Default.aspx is running there on IIS server and i put these two lines in Page_Load. I am using uprise way http post and set request body.
Dim sr as New StreamReader(Page.Request.InputStream)
Page.Reqeust.SaveAs("c:\my.xml", False)
if all you're doing is posting xml you dont need stream reader on the .net side of things. simply "POST" the xml from the client to the server (default.aspx) and receive the post on the server via (request.form("myXmlString")). Once you have the xml on the server just save it to a file.
hope that helps
john
__________________
---------------------------------------------------------------------- I love being a dad, flying airplanes and writing code.
----------------------------------------------------------------------
Follow me on Twitter: @BostonMerlin
Feed your brain on Twitter: @iPhoneDev101
----------------------------------------------------------------------
iPhone Apps:
i try with request.form("myXmlString") but it always return error in response. When i used this with simple text then it works, seems not able to parse <> tags.
Anyway i used XMLDoc to receive the Request.Inputstream and then doc.Save(""); to save it locally. That's i wanted to send the xml at server side.
so can I use the code to POST data? can I use this method to load a page that inserts POST data into a MySQL db? Id like to send an NSString to my server and enter it into a db. Trying this method I cannot get it to work..Any tutorials out there?
cheers.bo
NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
// setting up the URL to post to
NSString *urlString = @"http://iphone.zcentric.com/test-upload.php";
// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
/*
add some header info now
we always need a boundary when we post a file
also we need to set the content type
You might want to generate a random boundary.. this is just the same
as my output from wireshark on a valid html post
*/
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] 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]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
I was courious why everybody stop talking about upload audio files , and talk about posting Images ?
I think it is easy and reliable to upload image through HTTP POST request .
But it's hard to upload big files, such as audio files, mp4 files. Can we do this still through HTTP POST ? I guess not so.
Even we can do this, is there some progress bar to know how much of this file has been transferred ?
Good work.
Its work fine with php, but in RubyOnRails server I'm get the error:
Code:
Status: 500 Internal Server Error
bad content body
I'm looking for some solution and will post here when found.
Thank you
Any luck finding where this error was coming from? I'm having the same problem with CF/Java. It's giving me a status 500 invalid form data. It works fine with PHP, but not go with CF.
I have to do the same thing only with images. i'm running an asp.net app on the server side which needs to accept a posted image from the iphone.
anyone have a direction on this?
Thanks
John
Sir,
I have to deal with the same thing. i have to send the images from my iPhone App to the asp.net App. if you can help in this then it will be helpfull for me.
Mahesh