I have a form with 10 file inputs. They can contain 10 random files with random sizes.
If I send these files to ASP.NET server with this code:
var count = HttpContext.Current.Request.Files.Count;
var TotalSize = 0;
for (int i = 0; i < count; i++ )
{
HttpPostedFile postedFile = HttpContext.Current.Request.Files.Get(i);
TotalSize += postedFile.ContentLength;
}
And as you can see I didn’t save the files on the server, will this code just calculate the summary of files without need to receive the whole file from the client (And therefore it would be very fast)?
Your code above will not run until ALL of the files are loaded on the server, and therefore will not be fast (if the uploaded files are large).
There are ways to check the size before actually uploading the whole file, but not in your code of the page itself. You either have to use an HttpModule/Handler to do the upload, or use a Flash/Silverlight component to check the file size on the client BEFORE uploading.
Here’s a page with some good info on the problem and links to solutions.