Programmatically retrieve file size from remote file using delphi before downloading

How can I determine the size in bytes of a remote file hosted on the network before downloading it using Delphi?

Thanks in advance.

+6
delphi
source share
3 answers

You can use Indy.

IdHTTP .

You can get the size this way:

 procedure TFormMain.Button1Click(Sender: TObject); var Http: TIdHTTP; begin Http := TIdHTTP.Create(nil); try Http.Head('http://live.sysinternals.com/ADExplorer.exe'); ShowMessage(IntToStr(Http.Response.ContentLength)); finally Http.Free; end; end; 
+11
source share

Short answer: use the HTTP HEAD command available in the TIdHttp Indy Delphi component.

More details:

The HTTP protocol defines the HEAD method.

9.4 HEAD

The HEAD method is identical to the GET method except that the server SHOULD NOT return the message body in the response. the meta information contained in the HTTP headers in response to the HEAD request MUST be identical to the information sent in response to the GET request. This method can be used to obtain meta-information about an object implied by a request without transmitting the essential body itself. This method is often used to test hypertext links for validity, accessibility, and recent modification.

The response to the HEAD request MAY be cache in the sense that the information contained in the response MAY be used to update a previously cached object from this resource. If the new field values ​​indicate that the cached object is different from the current one (as indicated by a change in Content-Length, Content-MD5, ETag, or Last-Modified), then the cache MUST treat the cache entry as obsolete.

HEAD requests a response identical to the one corresponding to the GET request, but without the response body, receiving full response headers, without all the content.

The resulting HTTP response headers are documented in the Wikipedia HTTP header list. http://en.wikipedia.org/wiki/List_of_HTTP_headers

The HTTP headers form the core of the HTTP request, and are very important in the HTTP response. They determine the various characteristics of the data that is requested or the data that has been provided. The headers are separated from the request or the response body is an empty string. HTTP headers can be almost arbitrary strings, but only a few are commonly understood.

One of the headers that is always present for a valid content retrieval URL is the Content-Length header.

14.13 Content-Length

The Content-Length entity-header field indicates the size of the entity-body, in decimal OCTET, sent to the recipient, or, in the case of the HEAD method, the size of the entity body that would be sent if the request was GET.

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

An example is

Content-Length: 3495

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

Any content length greater than or equal to zero is a valid value. Section 4.4 describes how to determine the message body length if no Content-Length is specified.

Please note that the value of this field is significantly different from the corresponding definition in MIME, where this is an optional field used within the "Message / external-body" content type. In HTTP, it MUST be sent whenever the length of the message can be determined before transmission, unless prohibited by the rules in section 4.4.

From Delphi, drop the TIdHttp component into your form. And paste the following code into one of your delphi event handling methods.

 var url: string; // must contain a fully qualified url contentLength: integer; begin .... contentLength:=0; try Idhttp1.Head(url); contentLength:=idhttp1.response.ContentLength; except end; .... 
+10
source share

Remember that not ALL servers return a valid content size for a chapter request. If the content length = 0, you will ONLY know if you issue a GET request. For example, a HEAD request for a Google logo returns a content length of 0, however GET returns the correct length but also retrieves the image. Some servers will return the length of the content as the length of the packet following the header.

You can use Synapse to get this information. Note that data is being transmitted, but the buffer is discarded. This is a much more reliable method, but at the expense of additional bandwidth.

 var HTTP : tHTTPSend; begin HTTP := THTTPSend.Create; try HTTP.HTTPMethod('GET',url); DownloadSize := HTTP.DownloadSize; finally HTTP.Free; end; end; 
+3
source share

All Articles