Results 1 to 7 of 7
  1. #1
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    (VB) Downloading a file

    How do I programmatically download a file from a web site? I will be working in VB and I want to make a button that onclick() will retrieve a file from a web site and download it on to a user's computer.

    Ideally, I want no user interaction or warnings after the button is clicked. Perhaps I might add a brief message saying "X" file was downloaded to "Y" -- but I can do that. I just want the file download to occur without having the user see the Download Dialog box that one sees in IE.

    Yes, I am aware this could used to do nefarious things -- so please don't lecture me! <img src=/S/smile.gif border=0 alt=smile width=15 height=15> I am using this for the exact opposite purpose. I am helping a guy create a tool to download a new definition file for his security program by having the user simply push a button.

    Thanks for any advice.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: (VB) Downloading a file

    Take a look at <!post=MarkJ's response to my related question,299548>MarkJ's response to my related question<!/post> earlier this year. I never actually tried that; I can't even remember what I was working on. <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>

    Okay, I re-read my own post and I remember. What I ended up doing was creating an HTML file on the fly from various user criteria and writing it directly to disk using the FileSystemObject and the wshShell. How, you might wonder, could I overcome the IE security? Very simple. Rename the page with a .HTA (hypertext application) extension, and IE basically ignores it. The user answers an Open/Save question to get the HTA page to load in IE, and then it's completely trusted. (Read this for more info.) Of course, IE 5 or 6 is required to do it this way.

  3. #3
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: (VB) Downloading a file

    I will look into those suggestions later. Thank you.

    Let me clarify this a little more -- in case I need to make the plan more clear. A friend has this program that uses a text file as a "definition list" -- similar to the way Anti-Virus companies use definition lists. This list changes every few weeks. He posts the lists on a web site to be available for download. Presently, for users to "update" their program, they have to go to his web site, download the list, and the swap the New List for the Old List.

    Clearly, this is a behavior that can and should be automated -- just like Norton's LiveUpdate or even Microsoft's Windows Update. I want to create a button to do a "Live Update" of this definition file. It will not be an .html page. It might be listed as a ".txt" file, but I was actually thinking of creating a new file extension to use for this specific purpose.

    I know I can muck with the registry and make the new file extension and even give it a Mime Content Type for IE to use. That is all no problem. I just have never coded something to "Go to a site on the Internet, download file "X", and stick it in folder "Y". It should be relatively easy -- Norton's Live Update does it all the time!

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: (VB) Downloading a file

    I completely blew it on the context. Take a look at the Microsoft Internet Controls library, AKA shdocvw.dll (I think).

  5. #5
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: (VB) Downloading a file

    Did you try the URLDownloadToFile API? Example of use in VBA:

    Option Explicit

    ' REF: MSKB 244757 - HOWTO: Download a File Without Prompting

    Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
    "URLDownloadToFileA" _
    (ByVal pCaller As Long, _
    ByVal szURL As String, _
    ByVal szFileName As String, _
    ByVal dwReserved As Long, _
    ByVal lpfnCB As Long) As Long

    Public Sub TestURLDownloadToFile(ByRef strURL As String, ByRef strFile As String)

    Dim lngRtn As Long
    lngRtn = URLDownloadToFile(0, strURL, strFile, 0, 0)

    End Sub

    Examples:

    TestURLDownloadToFile "http://www.wopr.com/cgi-bin/w3t/showflat.pl?Cat=&Board=htm&Number=316448&page=0&vi ew=collapsed&sb=5&o=0&fpart=", "Cocuments and SettingsMARK DMy DocumentsTest.htm"

    TestURLDownloadToFile "http://www.wopr.com/w3tfiles/6-310581-modHwndStuff.txt","Cocuments and SettingsMARK DMy DocumentsHwnd.txt"

    NOTE: Both tests above worked OK; but as noted in MSKB 244757 - HOWTO: Download a File Without Prompting, "Note that when downloading HTML files, embedded content like images and objects will not be downloaded." In first test, all the images, etc do not appear in downloaded file, but text content appears OK. When I ran sub with same URL, but with "Test.txt" specified as file name, the resulting text file was the HTML source code, which may not be what you want... But if you download an URL that IS a text file, like the second example above, the resulting text file is same text as appears in browser, NOT the source code!! If it is a text file you want to download & update on local PC, this should work OK. Also note, any existing file with same name specified for strFile argument will be overwritten w/o warning (which may be what you want in this case). In any event you will have to test this to see if will work for what you are doing.

    HTH

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: (VB) Downloading a file

    The link to MSDN in that KB doesn't work, so here are some references:

    Top level: MSDN Home > MSDN Library > Networking > URL Monikers
    Function: URL Monikers > Reference > Functions > URLDownloadToFile Function

  7. #7
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: (VB) Downloading a file

    Thanks to both of you. I did run into this function -- and it is described fairly well here:

    Downloading Files using URLDownloadToFile

    I should be all set -- I appreciate the efforts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •