-1

Check if it is possible to download file with delphi

Good day ... I'm starting in Delphi and I am creating a program to download files from HTTP ...

I'm using:  

IdHTTP.Get (URL FileDownload)

But my program generates error when it can not download the file ...

Ex: http://exemplo.com/ -> Generates Error

Ex: http://exemplo.com/meudownload.ts -> Makes download

I'd like to correct this error, how can I check if it is possible to download the file or if it is being lowered ...

I used EIdHTTPProtocolExeption but only checks HTML error like: 404, 300 ... and is not that I wish ...

I would like to see if you can download the file, or if it is being downloaded ...

If this happens I will download other file:

donw_1.ts, down_2.TS, down_3.TS ... then when he gets el Archiving one that can not be downloaded, I have the answer to cancel the loop ...

user3185448
  • 81
  • 1
  • 10
  • What error did you receive? Please post the whole error message – Sir Rufo Jan 11 '14 at 16:55
  • "or if it is being downloaded" - that information should be kept in some list or array within your file. Server has no sure means to detect if you are downloading the file or not. Nor do many servers have a reason to know it. Thus - you cannot ask the server about it, you should track yourself what your program is doing – Arioch 'The Jan 11 '14 at 19:27
  • 1
    "to see if you can download the file" you just try to download the file. If you downloaded it, then it can be downloaded. If you failed to download it, then it can not be downloaded. – Arioch 'The Jan 11 '14 at 19:30
  • I could have sworn I asked this question before and had a good detailed answer, but can't find it now. – Jerry Dodge Jan 12 '14 at 08:00

3 Answers3

6

Catching the EIdHTTPProtocolException is what you should be doing. Remember that Indy is specifically designed to make use of exceptions for error reporting. EIdHTTPProtocolException is how TIdHTTP reports HTTP-specific errors from the server (any other type of exception means either the socket or the RTL had a problem and the HTTP request can be retried). The mere fact that you get that exception means the server could not deliver the file as requested, so you typically should not retry unless you know it is a recoverable error. Checking the exception ErrorCode is optional, but available if you really need to differentiate between specific errors.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
2

If you just want to see if a file exists on the server, use TIdHTTP.Head. This function just downloads the headers that would have been returned from the equivalent Get call, but not the data itself. You can then check the headers to see if the file exists and decide what to do next.

Anders E. Andersen
  • 1,635
  • 2
  • 14
  • 20
0

Unable to solve the problem by doing a scan HEAD ... and managed to solve the problem with the HTTP 404 error .... where there was no file to download it generates this error EIdHTTPProtocolExeption ... the problem has been resolved ....

user3185448
  • 81
  • 1
  • 10
  • 1
    It is galling that you ask for help, get it, but then accept your own low quality answer. @Remy has spent time giving you an excellent answer. Do you realise he is an author of Indy? You should accept his answer. You have done this over and again here. It is very ungrateful. – David Heffernan Feb 28 '14 at 04:25