1

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?

Alberto
  • 199
  • 2
  • 17
  • 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 Answers1

5

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:

  1. Send a separate HEAD request to the URL first and check the Content-Disposition response header, if present. You can use HttpSendRequest() and HttpQueryInfo() for that, or any other HTTP library. You can then format a filename as needed, and then download the URL to that filename.

  2. 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 from URLDownloadToFile() you have to write a class that implements the IBindStatusCallback and IHttpNegotiate COM interfaces, then pass an instance of that class to the lpfnCB parameter. The response headers will be passed to your IHttpNegotiate.OnResponse() implementation.

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