views:

308

answers:

2

Ok this may seem like a bit of a noob question but one many Web developers I know dont have a full understanding of.

Basically how does a file uploading from a web page file input box to a webserver (hosting a .net site for example) with a FileUpload control's uploaded event?

My understanding was that a post was made to the server requesting a stream, which is then passed back to the browser and streaming of the data to the server begins.

My friend says his understanding was that the whole file is encoded into the post request by the browser and the (massive) post is then sent to the server.

I thought this couldn't be the case as if so there would be no way to build an ajax progress bar as the server would not be able to do anything until it had received the entire post by which time it might as well just save the file to its disk.

So how does it actually work from browser to server?

+6  A: 

I think your friend is right, the file is encoded into the post sent to the server. If you really want to see exactly how it works, try using Tamper Data in Firefox to view the actual post data.

ETA:

The AJAX style uploads you mention work by doing the post in a hidden iframe, then using AJAX requests to check the upload progress on the server side.

Eric Petroelje
OK what my mate is saying I am pretty sure is right but surely the httphandler has to receive the post before it can do anything? And by this time it has the entire post anyway. or does a httphandler work on the network level and intercept packets could you explain further?
Sheff
Some servers can start processing a request after the headers are read but before the data is read. Some of the frameworks I've used give the body of a request as a stream (unless headers and such are parsed). Most request handlers, of course, don't send a response back until the request is read completely.
Kathy Van Stone
I'm not sure that the entire post has been downloaded at the point where ProcessRequest() is called on your httphandler, but I couldn't find any real documentation to say one way or the other.
Eric Petroelje
This is the problem I came across.
Sheff
+7  A: 

Your friend is right. If you want an AJAX progress bar, you have to jump through some hoops.

Usually the technique is to post the upload inside an iframe to an IHttpHandler on the server that stores progress on the server in a server-wide dictionary keyed by an identifier the client makes up and includes in both the AJAX progress request and the upload post. That way, when the client makes the AJAX requests, the server code processing that request can read the value from that dictionary to see how many bytes of the file POST request the server has processed.

Yes, it's complicated :)

Jonathan
I think this makes sense, but it suggests that the httphandler works on a network level and can detect incoming packets of a partial post?
Sheff
HttpHandlers don't work 'on the network level' persay, but they can do work at a lower-level than a standard page. On a normal page, until the POST has been full loaded on the server, your event-handling code doesn't run. In an IHttpHandler, you can process the data as it arrives, but you lose all the automatic stuff the page lifecycle does for you.
Jonathan