|Nov 22, 2012, 11:03 AM||#1|
Uploading file using NSURLConnection
In my app developed in Lion using XCode, I need to determine the speed of the internet connection (upload speed) and for that I upload an file using NSURLConnection.
I extracted the code for formatting the xml and uploading from
I am using the async version of NSURLConnection to free up the main UI and also as I want the progress of the uploading process.
I have implemented the delegate methods provided by NSURLConnection they are.
//1. Method fired after upload is complete along with response code. - (void)connection:(NSURLConnection *)theConnection didReceiveResponse:(NSURLResponse *)response //2. Method fired when upload process fails. - (void)connection:(NSURLConnection *)theConnection didFailWithError:(NSError *)error //3. Method fired when upload process is completed. - (void)connectionDidFinishLoading:(NSURLConnection *)theConnection //4. Fired periodically indicating no. of bytes sent/uploaded to the server (can be used to determine upload speed). - (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
However during the end of the upload process with an invalid upload URL(when totalBytesWritten == totalBytesExpectedToWrite), the method 1. fires with an error http 404 indicating that the upload process has failed.
Is there a mechanism by which I can find whether the uploading of is being carried out successfully during the actual uploading of the file rather than waiting till the entire file is uploaded?.
|Nov 25, 2012, 05:19 AM||#2|
When you send a HTTP request, even if the request is invalid you get a response.In the response message there is written the error code.In this case the error is 404 so probably the URL is wrong.Hope it helps.
|Nov 25, 2012, 09:17 AM||#3|
Yes. You are right.
But i find that the http response arrives only after the entire file is uploaded (even though the upload was never successful).
I don't get any http response stating an error for the current upload (as the url is invalid) during the initiation of the upload or during the upload process itself.
Only after the entire file is uploaded (after which i receive the http resone) can I determine if the file upload was successful or not.
Is there an mechanism by which I can figure if the upload url is invalid or not without having to upload an entire file?
|Nov 25, 2012, 01:14 PM||#4|
If you don't know how the protocol actually works, you should probably study that. It's not a complex protocol in its typical use. Only the 1xx responses really have any degree of complexity for handling them.
As a strategy for using HTTP, you could send a small POST or PUT request first, and see if it works. If it works, only then do you send a larger POST or PUT. If the small request fails, you can reasonably predict the larger request will also fail.
In any case, this assumes you have some control over what the HTTP server is doing. If you have no control over the server at all, then you have no way to control whether it even accepts a POST or PUT request, much less how it responds to them.
It's unclear what the overall context of this upload is, and what kind of error handling (e.g. failure responses from server) your program actually needs to perform.
Any server will have constraints, and if your program doesn't work within them, your uploads will fail. So if you don't have a strategy for working within the constraints, by designing your program to handle those constraints, then you will eventually encounter an error you can't handle, and your program will fail. Even a public web-service like AWS S3 has constraints on uploads. If you don't know what your server's constraints are, then you have more research to do.
|Nov 27, 2012, 07:10 AM||#5|
Thanks for the reply.
Yes. The alternate approach that I had in mind to resolve this issue was for me to upload an small file (few kb's) first and check for the http response. This will indicate whether the url is valid or not.
Though this approach has a little overhead in the form of additional time it takes, looks like i don't have any alternatives either.
|Thread Tools||Search this Thread|
|thread||Thread Starter||Forum||Replies||Last Post|
|Uploading to YouTube||WerePig||iPod touch||1||Feb 21, 2014 01:00 AM|
|Resolved: AFNetworking Vs NSURLConnection ?||Tander||iPhone/iPad Programming||3||Jul 30, 2013 04:36 AM|
|Error handling in NSURLConnection sendAsynchronousRequest||waterskier2007||iPhone/iPad Programming||1||Mar 1, 2013 08:57 AM|
|Please help : Uploading||WhySoSerious||iPad||3||Aug 21, 2012 09:50 PM|
|Uploading pictures||migdaddy||iPad||3||Jun 9, 2012 11:34 PM|
All times are GMT -5. The time now is 05:10 AM.