views:

157

answers:

3

I have a very simple page with which a user uploads a file. Using only server-side C# (e.g. the code-behind), how can I measure the amount of time the user had to wait for the file to upload?

+1  A: 

This is very difficult (well, it was in IIS6). You need to write an HttpModule to intercept the upload and handle it, then using the total size of the file coming in (it's in the header of the request), calculate how many bytes per second you're getting, and then you can calculate how much time is left.

However, you cannot do any of this with just the code-behind file -- the file is already uploaded and available by the time the request gets to the asp.net handler.

Robert C. Barth
You've told me how to calculate speed, not duration? Is that right? Your last paragraph is relevant -- saying that ASP.NET doesn't know about the request until it's already completely uploaded to the server?
lance
No, I told you how to measure duration. Once you have bytes per second, you can calculate total time if you know the total bytes of the file. E.g. if you're getting 4,000 bytes per second throughput, and the file is 16,000 bytes, it's going to take four seconds total (estimate; these calculations are never entirely correct until they're done). Implicit in this is that you need to maintain total total bytes received and seconds elapsed since start -- divide the two to get bytes per second, then divide the file size in bytes by bytes per second to get the number of seconds for the whole file.
Robert C. Barth
The method I described is used for predictive estimation of elapsed time (e.g. one would usually then subtract the total time from total time to give the user a "seconds left" or whatever prompt). It would be helpful if we knew what you're doing with the data... if you're just using it for back-end reporting purposes, the solution by Mr. Fisher should suffice.
Robert C. Barth
A: 

I had to find something like this and I'm using this.

Fredou
What on that page tells me how long an upload took?
lance
the progress bar
Fredou
+1  A: 

If you're not concerned with complete accuracy, you can send a quick call to the server to indicate that an upload is starting, followed immediately by the upload request. The server can then measure the time between the first call and the completion of the second call.

John Fisher
This isn't as easy as it sounds, but I'm going to up vote anyway. Take for instance the case where two users are uploading files at the same time -- the server needs to be able to distinguish between unique file start times. Doable, but not trivial. And since the first call will most likely be an out-of-band AJAX-type call, the data structure on the server will need to be static, and then he'll need to clear it out, etc.
Robert C. Barth