6

I've been trying to create (TFileStream) a PDF through the TDownloadURL class, but I'm really having troubles in obtaining the file/stream from the URL, specially if the URL a HTTPS.

I'm not sure if I was clear, but I will post a snippet so it might help understanding:

implementation
var pdfStreamed: TDownloadUrl;
var fileStream : TFileStream;
  procedure generateStream;
  begin
    pdfStreamed:= TDownLoadURL.Create(nil);
    with pdfStreamed do
      begin
        URL := 'https://farm9.staticflickr.com/8327/8106108098_08e298f0d9_b.jpg'; //stream;
        FileName := 'D:\';
        ExecuteTarget(nil);
//        Execute;
      end;
  end;

The URL property exists both in HTTP as in HTTPS! But it throws me an error: Exception class Exception with message 'Error downloading URL: https://farm9.staticflickr.com/8327/8106108098_08e298f0d9_b.jpg'.

Could point what am I doing wrong? I've searched a lot for this, but couldn't find anything that work and simple!

Thanks a lot!

Armando Freire
  • 417
  • 2
  • 7
  • 19

2 Answers2

7

TDownloadURL is just a thin wrapper around Microsoft's URLDownloadToFile() function, which supports HTTPS just fine.

TDownloadURL does not tell you why URLDownloadToFile() fails, unfortunately. However, I can see that you are setting the FileName property to just a folder path, but you need to instead set it to the full path and filename of the destination file that is going to be created to hold the downloaded data. IOW, change this:

FileName := 'D:\';

To this:

FileName := 'D:\8106108098_08e298f0d9_b.jpg';
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Hey @Remy thanks for the reply, but I've already tried this solution and it didnt work! It keeps displaying "Error downloading URL". I understood it's just a wrapper, but could the Deplhi affect this call the way It would throw me an error? :\ By the way, if I replace the HTTPS with HTTP it works! But I really need the HTTPS =\ – Armando Freire Jan 14 '13 at 20:31
  • The error message you are seeing means `URLDownloadToFile()` itself is failing, not `TDownloadURL`. It is just a plain ordinary API function call. If you want to find out why `URLDownloadToFile()` is failing, you need to call it directly so you have access to its resulting error code, which `TDownloadURL` does not expose access to. – Remy Lebeau Jan 14 '13 at 21:09
3

Use Remy's answer of changing the file name to specify the correct place to save, but to fix, change your ExecuteTarget line to something like

ExecuteTarget(Self);

I just tried your code with those two changes, and it successfully downloaded the image. Essentially, the component needs a handle to reference as from Here

Tom A
  • 1,662
  • 2
  • 23
  • 41