I use URLDownloadToFile to download a file in Delphi. In the url there is not the real name of the file. Is it possible to specify just the path of the file, keeping the default name that i.e. Explorer show?
-
File name supposed to be in `Content-Disposition` header. You'll may need to provide more details in your question. – Free Consulting May 23 '14 at 10:02
-
I'm in the context of a Windows Client application, I have an URL (I have to built it with some parameters) and just call URLDownloadToFile. The problem is that I have to pass the name of the destination file, not just the URL. – Alberto May 23 '14 at 12:12
1 Answers
You are in a catch-22 situation. You need to give URLDownloadToFile()
a filename, but you have to request the URL first to discover if it has its own filename.
You have two choices:
Send a separate
HEAD
request to the URL first and check theContent-Disposition
response header, if present. You can useHttpSendRequest()
andHttpQueryInfo()
for that, or any other HTTP library. You can then format a filename as needed, and then download the URL to that filename.Use a temp filename for the download, then check the
Content-Disposition
response header, if present, and rename the file if needed. To get the response headers fromURLDownloadToFile()
you have to write a class that implements theIBindStatusCallback
andIHttpNegotiate
COM interfaces, then pass an instance of that class to thelpfnCB
parameter. The response headers will be passed to yourIHttpNegotiate.OnResponse()
implementation.

- 555,201
- 31
- 458
- 770