Results 1 to 13 of 13

Thread: trying to get around the "File Download" dialog box in internet explorer

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    154

    trying to get around the "File Download" dialog box in internet explorer

    When you click on a file in internet explorer (file.exe for example) then the "File Download" dialog box comes up. I have a webbrowser control in my program, and when people click on "file.exe" then I don't want the "file download" dialog to appear, I just want it to automatically download to the desktop. An input box that says "Are you sure you want to download 'file.exe'?" would be better, but If you tell me the first thing then I can add the inputbox after.

    Thanks in advance.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You can combine the URLDownloadToFile API with the FileDownload event.
    You must remeber the last URL though, save it in a variable in the BeforeNavigate2 event.
    Code:
    Private Declare Function URLDownloadToFile _
     Lib "urlmon.dll" Alias "URLDownloadToFileA" ( _
     ByVal pCaller As Long, _
     ByVal szURL As String, _
     ByVal szFileName As String, _
     ByVal lpfnCB As Long) As Long
    
    Private sUrl As String
    
    Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
        sUrl = URL
    End Sub
    
    Private Sub WebBrowser1_FileDownload(Cancel As Boolean)
        Dim sFileName As String
        
        Cancel = True
        sFileName = Mid$(sUrl, InStrRev(sUrl, "/") + 1)
        'change the following to the folder you
        'want to save the file to
        sFileName = "c:\" & sFileName
        If MsgBox("Are you sure you want to download this?", _
         vbQuestion + vbYesNo) = vbYes Then
            URLDownloadToFile 0, sUrl, sFileName, 0
        End If
    End Sub
    Best regards

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    154
    I used this exact code:

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

    Private sUrl As String

    Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
    sUrl = URL
    End Sub

    Private Sub WebBrowser1_FileDownload(Cancel As Boolean)
    Dim sFileName As String

    Cancel = True
    sFileName = Mid$(sUrl, InStrRev(sUrl, "/") + 1)
    'change the following to the folder you
    'want to save the file to
    If MsgBox("Are you sure you want to download this?", _
    vbQuestion + vbYesNo) = vbYes Then
    URLDownloadToFile 0, sUrl, "c:\" & sFileName, 0
    End If
    End Sub
    --------------------------------------------------------------------------------


    The msgbox comes up and asks.. and I hit yes.. but then I go check c:\ and there are no files there. Can somebody tell me whats wrong with the code?

  4. #4
    New Member
    Join Date
    Sep 2005
    Posts
    1

    Re: trying to get around the "File Download" dialog box in internet explorer

    I copied the code above and the event filedownload dosen't fire




    what's wrong???

  5. #5
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: trying to get around the "File Download" dialog box in internet explorer

    Where did you copy it?

  6. #6
    Addicted Member
    Join Date
    Mar 2006
    Posts
    186

    Re: trying to get around the "File Download" dialog box in internet explorer

    VB Code:
    1. Private Declare Function DoFileDownload Lib "shdocvw.dll" (ByVal lpszFile As String) As Long
    2.  
    3. Private Sub Form_Load()
    4.    DoFileDownload StrConv("http://www.server.com/download/file.zip", vbUnicode)
    5. End Sub

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: trying to get around the "File Download" dialog box in internet explorer

    @foxter: You are aware that this thread was 5 years old, aren't you?

  8. #8
    Addicted Member
    Join Date
    Mar 2006
    Posts
    186

    Re: trying to get around the "File Download" dialog box in internet explorer

    People dont [RESOLVE] the thread, and dont post the answer they got. How do you think other people will get the answer? Do others need to ask again the same question? I'd rather post the solution, so others would have it, no matter how old the problem is.

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: trying to get around the "File Download" dialog box in internet explorer

    Well, 5 years ago it wasn't as common for people here to put the word "RESOLVED" in the title. Besides, using DoFileDownload as you suggested does not "get around the 'File Download' dialog box" which was the origional question. Using URLDownloadToFile as I suggested in post #2 above (5 years ago) would however do that.

  10. #10
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: trying to get around the "File Download" dialog box in internet explorer

    Also, Cancel = True doesn't prevent the normal download dialog box from appearing. Don't even know what it's purpose is. However, the code for something like this should be placed in the _BeforeNavigate2 event because there Cancel = True will prevent the download box from popping up.
    Last edited by jmsrickland; Sep 24th, 2008 at 02:00 PM.

  11. #11
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: trying to get around the "File Download" dialog box in internet explorer

    This will bypass the dialog box:
    Add reference to WinHTTP.dll in systems32. (Browse to it, if Microsoft WinHTTP services doesn't show on the references list.)

    Note this simple example doesn't do anything fancy, just dumps the downloaded bytes to disk.

    OnResponseStart when you can safely access the headers.
    OnResponseDataAvailable which returns a byte array of downloaded data for each chunk.
    OnResponseEnd which ends the download.


    Code:
    Option Explicit
    Private WithEvents http As WinHttpRequest
    Private mContentLength As Long
    Private mProgress As Long
    
    Private Sub Command1_Click()
        ' Create the WinHTTPRequest ActiveX Object.
        Set http = New WinHttpRequest
        ' Open an HTTP connection.
        http.open "GET", "http://home.in.tum.de/~paula/mpeg/wg_gdo_1.mpg", True  'True means asynch.
        ' Send the HTTP Request.
        http.send
    End Sub
    
    Private Sub http_OnResponseDataAvailable(Data() As Byte)
        mProgress = mProgress + UBound(Data) + 1
        ProgressBar1.Value = mProgress
       
        Put #1, , Data
       
       
    End Sub
    
    Private Sub http_OnResponseFinished()
        Close #1
        MsgBox "done"
    End Sub
    
    Private Sub http_OnResponseStart(ByVal Status As Long, ByVal ContentType As String)
        Text1.Text = http.getAllResponseHeaders()
        mProgress = 0
        mContentLength = CLng(http.getResponseHeader("Content-Length"))
       
        ProgressBar1.Max = mContentLength
        Open "C:\twg_gdo_1.mpg" For Binary As #1
    
    End Sub
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  12. #12
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: trying to get around the "File Download" dialog box in internet explorer

    This will bypass the dialog box:

    What dialog box are you refering to?

  13. #13
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: trying to get around the "File Download" dialog box in internet explorer

    Quote Originally Posted by jmsrickland
    This will bypass the dialog box:

    What dialog box are you refering to?
    The ie download dialog
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

Posting Permissions

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



Click Here to Expand Forum to Full Width