• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 710
  • Last Modified:

Loading a file into memory from a URL

I have created an ActiveX plugin for Internet Explorer for viewing Tiff files.
I can view Tiff Files with filename like "\\Servername\Images\image.tif", but what I would like to do is view tiff files with filename like "http://www.webserver.co.uk/images/image.tiff".

The solution I'm looking for, is where the Tiff file is loaded straight into a TMemoryStream from the webserver.
0
StayGreedy
Asked:
StayGreedy
3 Solutions
 
DeerBearCommented:
Hi,

Use IdHTTP1.Get from Indy, you can pass a buffer stream as parameter and it should work.

HTH,

Andrew
0
 
Mohammed NasmanSoftware DeveloperCommented:
Something like that

procedure TForm1.Button1Click(Sender: TObject);
var
  ImgPath : string;
  MS : TMemoryStream;
begin
  ImgPath := 'http://www.experts-exchange.com/images/logo.gif';
  MS := TMemoryStream.Create;
  idHttp1.Get(ImgPath,MS);
// if u don't want to save the img to harddisk remove this line
  MS.SaveToFile('c:\img.gif');
  MS.Free;
end;
0
 
mocartsCommented:
hi
there is also possible to use wininet.pas unit provided with delphi (no need for install Indy, just copy in your sorce and use):

uses wininet;

function GetStreamFromURL(const aURL: string; aStream: TStream): boolean;
const
  lBufSize = 4096;
var
  hi, hu: HINTERNET;
  buf: array [1..lBufSize] of byte;
  dwcb: dword;
begin
  Result := False;
  aStream.Size := 0;
  hi := internetOpen('StreamFromURL', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if assigned(hi) then
  try
    hu := internetOpenURL(hi, PChar(aURL), nil, 0, INTERNET_FLAG_KEEP_CONNECTION, 0);
    if assigned(hu) then
    try
      repeat
        Result := internetReadFile(hu, pointer(@buf), lBufSize, dwcb);
        if Result then
          aStream.Write(buf, dwcb);
      until not Result or (dwcb <> lBufSize);
    finally
      internetCloseHandle(hu);
    end;
  finally
    internetCloseHandle(hi);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  fs: TMemoryStream;
begin
  fs := TMemoryStream.Create;
  try
    if GetStreamFromURL('http://home.fortech.lv/~helmuts/delphi/PalmDB.zip', fs) then
    begin
      fs.SaveToFile('D:\PalmDB.zip');
      Caption := 'ok';
    end;
  finally
    fs.Free;
  end;
end;

wbr, mo.
0
 
StayGreedyAuthor Commented:
Thanks to DeerBear for idea of using the indy component.
Thanks to mnasman for the indy example
Special thanks to mocarts for the alternative example, the one I am using :)

Cheers.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now