简体   繁体   中英

How to calculate the percentage of a file uploaded?

I need to calculate the percentage of a file uploaded to the server. I read it like

Percents uploaded=100/file size*bytes uploaded.

Does anyone know how we can get the following from the properties of a file upload control:

  1. Uploaded File's size
  2. Bytes uploaded

Please help. Thank You

"Does anyone know how we can get the following from the properties of a file upload control"

If, as you've said in the comments, you are using the standard .NET File upload controls, then you only get the server-side post event when the file has been completely uploaded.

That is: None of your server-side events will fire until the file has been completely uploaded by the browser.

To get progressive uploads, you either need to use a component such as the ones linked in other answers - or alternatively, write your own HTTP Module.

Example project (unverified if working) http://www.codeproject.com/Articles/14832/ASP-NET-file-post-direct-to-disk-with-upload-file

Side note: Progress % is calculated like so: uploaded bytes/total bytes

The HTML specification up to version 4, does not allow you to upload files without page post, you need to rely on server code to get the amount of data uploaded, however HTML5 specification comes with new features for XMLHttpRequest known as XMLHttpRequest2. There's an upload property for XMLHttpRequest in which you can have access to some client-side events for uploading data, like onprogress, onload and so forth. you can read it here: http://www.html5rocks.com/en/tutorials/file/xhr2/

Here's a the compatibility table for support of XMLHttpRequest 2 in all major browsers: http://caniuse.com/xhr2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM