Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

URLDownlloadToFile How to clear Cache First?

Posted on 2006-07-15
8
3,367 Views
Last Modified: 2008-01-09
Dear experts,

I'm using the 'URLDownloadToFile' to download a file...and it works fine...the issue is that I use it to download a binary
update from a webserver ...but if I don't clear the cache first...the file is not downloaded frome the server and is taken
from the cache...

is there a way to delete that file from the cache first, and then download it? Or to force a download even if the file is
in the cache?

Thank you!

Dave
0
Comment
Question by:dcrudo
  • 3
  • 3
  • 2
8 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 17114046
If you have a .php (or some other file) that throws the binary file for download put these headers:

<?php
header('Cache-Control: no-cache, no-store, must-revalidate'); //HTTP/1.1
header('Expires: Sun, 01 Jul 2005 00:00:00 GMT');
header('Pragma: no-cache'); //HTTP/1.0
?>

This should force the download from the server and skip the cache.
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17114525

The flags I am posting do not work with UrlDownloadToFile (router trace shows no difference), but in using UrlDownloadToCacheFile they do work (since v4.01 sp2 of Internet explorer). By passing the flag BINDF_GETNEWESTVERSION you can force the url handling to recheck the site for a newer version, vs reading the data from the cache. More info can be obtained here:

http://support.microsoft.com/kb/q196466/

Below is an example of using the URLDownloadToCacheFile function. The only real difference is that the function gives you the file name instead of you specifying one. (But you could always use CopyFile to move it.). And because you get back the cache file name, you can delete it using DeleteFile, etc if you wanted.

Regards,
Russell


const
  BINDF_ASYNCHRONOUS            = $00000001;
  BINDF_ASYNCSTORAGE            = $00000002;
  BINDF_NOPROGRESSIVERENDERING  = $00000004;
  BINDF_OFFLINEOPERATION        = $00000008;
  BINDF_GETNEWESTVERSION        = $00000010;
  BINDF_NOWRITECACHE            = $00000020;
  BINDF_NEEDFILE                = $00000040;
  BINDF_PULLDATA                = $00000080;
  BINDF_IGNORESECURITYPROBLEM   = $00000100;
  BINDF_RESYNCHRONIZE           = $00000200;
  BINDF_HYPERLINK               = $00000400;
  BINDF_NO_UI                   = $00000800;
  BINDF_SILENTOPERATION         = $00001000;
  BINDF_PRAGMA_NO_CACHE         = $00002000;
  BINDF_GETCLASSOBJECT          = $00004000;
  BINDF_RESERVED_1              = $00008000;
  BINDF_FREE_THREADED           = $00010000;
  BINDF_DIRECT_READ             = $00020000;
  BINDF_FORMS_SUBMIT            = $00040000;
  BINDF_GETFROMCACHE_IF_NET_FAIL= $00080000;
  BINDF_FROMURLMON              = $00100000;
  BINDF_FWD_BACK                = $00200000;
  BINDF_PREFERDEFAULTHANDLER    = $00400000;
  BINDF_RESERVED_3              = $00800000;

procedure TForm1.Button2Click(Sender: TObject);
var  dwMark:        LongWord;
     bSuccess:      Boolean;
     lpszFile:      Array [0..MAX_PATH] of Char;
begin

  dwMark:=GetTickCount;
  try
     // With this version, the file will get pulled from cache
     // bSuccess:=(URLDownloadToCacheFile(nil, 'http://www.yahoo.com', @lpszFile, MAX_PATH, 0, nil) = S_OK);
     //
     // This version re-checks the site for a newer version
     bSuccess:=(URLDownloadToCacheFile(nil, 'http://www.yahoo.com', @lpszFile, MAX_PATH, BINDF_GETNEWESTVERSION, nil) = S_OK);
  finally
     dwMark:=GetTickCount-dwMark;
  end;
  ShowMessage(Format('Download completed with value %d in %d ms', [Ord(bSuccess), dwMark]));
  if bSuccess then
     ShowMessage(Format('Your bits are located in the following file: %s', [lpszFile]));

end;
0
 
LVL 1

Author Comment

by:dcrudo
ID: 17120667
Hello,

I've tried to use the following:

URLDownloadToFile(nil,
                     'http://www.mysite.com/updates/program.exe',
                     Pchar(ExtractFilePath(application.Exename)+'program.exe'),
                     BINDF_GETNEWESTVERSION , bsc);

should not give the same result?
But the file is not downloaded again :(

any idea?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:dcrudo
ID: 17120673
Hi Ivanov,

but then how do I push the file back to the client? ...

Thx!
Dave
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 17120935
For example like this:

<?php
  header('Cache-Control: no-cache, no-store, must-revalidate'); //HTTP/1.1
  header('Expires: Sun, 01 Jul 2005 00:00:00 GMT');
  header('Pragma: no-cache'); //HTTP/1.0

  // We'll be outputting a PDF
  header('Content-type: application/pdf');

  // It will be called downloaded.pdf
  header('Content-Disposition: attachment; filename="downloaded.pdf"');

  // The PDF source is in original.pdf
  readfile('original.pdf');
?>
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17121606

What part of this:

>> The flags I am posting do not work with UrlDownloadToFile (router trace shows no difference), but in using UrlDownloadToCacheFile they do work (since v4.01 sp2 of Internet explorer).

did you not understand? Let me rephrase it; you MUST use UrlDownloadToCacheFile because the flags DO NOT WORK with UrlDownloadToFile

Russell

0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 17122308

const
  BINDF_ASYNCHRONOUS            = $00000001;
  BINDF_ASYNCSTORAGE            = $00000002;
  BINDF_NOPROGRESSIVERENDERING  = $00000004;
  BINDF_OFFLINEOPERATION        = $00000008;
  BINDF_GETNEWESTVERSION        = $00000010;
  BINDF_NOWRITECACHE            = $00000020;
  BINDF_NEEDFILE                = $00000040;
  BINDF_PULLDATA                = $00000080;
  BINDF_IGNORESECURITYPROBLEM   = $00000100;
  BINDF_RESYNCHRONIZE           = $00000200;
  BINDF_HYPERLINK               = $00000400;
  BINDF_NO_UI                   = $00000800;
  BINDF_SILENTOPERATION         = $00001000;
  BINDF_PRAGMA_NO_CACHE         = $00002000;
  BINDF_GETCLASSOBJECT          = $00004000;
  BINDF_RESERVED_1              = $00008000;
  BINDF_FREE_THREADED           = $00010000;
  BINDF_DIRECT_READ             = $00020000;
  BINDF_FORMS_SUBMIT            = $00040000;
  BINDF_GETFROMCACHE_IF_NET_FAIL= $00080000;
  BINDF_FROMURLMON              = $00100000;
  BINDF_FWD_BACK                = $00200000;
  BINDF_PREFERDEFAULTHANDLER    = $00400000;
  BINDF_RESERVED_3              = $00800000;

function DownloadToFile(URL, FileName: String): Boolean;
var  lpszFile:      Array [0..MAX_PATH] of Char;
begin

  // Download to cache by getting newest version
  if (URLDownloadToCacheFile(nil, PChar(URL), @lpszFile, MAX_PATH, BINDF_GETNEWESTVERSION, nil) = S_OK) then
     // Copy to desired file name
     result:=CopyFile(@lpszFile, PChar(FileName), False)
  else
     // Failed
     result:=False;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin

  DownloadToFile('http://www.mysite.com/updates/program.exe', ExtractFilePath(application.Exename)+'program.exe');

end;
0
 
LVL 1

Author Comment

by:dcrudo
ID: 17435092
It works! Thank you!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Machine not responding during CopyFile() 3 104
Help on project with Soap 10 57
How to convert memory stream to PDF file 6 111
Delphi: barcode reading on android platform 1 51
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question