How to set the file size in a Download response?


I have an application where I give a response by downloading a certain file on the server to the client. I zipo the image files that are in a folder on the server and give a response by manipulating the headers so that the file is downloaded.

I noticed that when this generated file is slightly larger, there is a delay to download it, but the remaining time to download based on the file size is not displayed in the browser.

An example written in Aspx WebForm:

string fullpath = GenerateZippedPhotos(id);

response.ContentType = "application/zip";

response.AddHeader("content-disposition", "");



To correct the problem described above, what do I need to do?

Is there any way to "tell the browser" the size of the file being downloaded?

asked by anonymous 30.11.2017 / 16:48

1 answer



response.AddHeader("Content-Length", "tamanho" );

Remembering that the size is in bytes, and only the body of the request, without the headers. Usually functions that deal with filesystem get this easily.

In C # (source) :

long tamanho = new System.IO.FileInfo("").Length;

Definition of W3:

  14.13 Content-Length      

The Content-Length entity-header field indicates the size of the   entity-body, in decimal number of OCTETs, sent to the recipient or, in   the case of the HEAD method, the size of the entity-body that would   have been sent the request to GET.

   Content-Length    = "Content-Length" ":" 1*DIGIT

An example is

   Content-Length: 3495

Applications SHOULD use this field to indicate the transfer-length of   the message-body, unless this is prohibited by the rules in section   4.4.

What, in summary is:


The Content-Length header indicates the body size in octets (8-bit units) that will be sent to the recipient, or in the GET in case of HEAD queries.


Applications should submit this field, unless prohibited by the rules in section 4.4

More on: link

Note: As mentioned by colleague @Jefferson in the comments, there are situations where it is not desirable to determine the file size beforehand. An example is the generation of streams , so you do not need to store everything in memory or on disk before you start sending. stream can be generated while already sent, saving memory and time.

30.11.2017 / 16:53