Results 1 to 5 of 5

Thread: File Download Problem

  1. #1

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    File Download Problem

    I use the below code to check and download updates for my app. The problem i've run into is if a user has a Download Manager enabled (eg. DAP), the Downlad Manager downloads the file and overwrites my code as to where the file should be downloaded to.

    Is there a way I can disable the Download Manager or another way to over come my problem?

    VB Code:
    1. Option Explicit
    2. Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
    3.     "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, _
    4.     ByVal szFileName As String, ByVal dwReserved As Long, _
    5.     ByVal lpfnCB As Long) As Long
    6.  
    7. Private Sub Form_Load()
    8. Dim url As String
    9. Dim localFileName As String
    10. Dim errcode As Long
    11.  
    12.  
    13. url = "http://www.mysite.com/myfile.txt"
    14. localFileName = App.Path & "\myfile.dat"
    15. errcode = URLDownloadToFile(0, url, localFileName, 0, 0)
    16.  
    17. End Sub

  2. #2
    Hyperactive Member Disiance's Avatar
    Join Date
    Sep 2004
    Location
    Denver, CO
    Posts
    439
    Don't use the URLDownloadToFile API *gasp!*

    Plugin a winsock control instead, connect on port 80, send the nessicary headers and download.

    Disiance

  3. #3
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994
    Would you mind telling him what the headers are instead of being a b*tch about it? I'm interested in this information as well.
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  4. #4
    Hyperactive Member Disiance's Avatar
    Join Date
    Sep 2004
    Location
    Denver, CO
    Posts
    439
    Originally posted by vbNeo
    Would you mind telling him what the headers are instead of being a b*tch about it? I'm interested in this information as well.
    I thought maybe you could take an initiative and look it up yourself, guess not. Anywho, connect to the server and send the following:

    Code:
    GET /filename.extension HTTP/1.1
    This tells the server what file you're after, starting after the domain name. e.g., if the file is http://www.domain.com/folder/file.zip then this line would read
    GET /folder/file.zip HTTP/1.1

    Code:
    Host: www.google.com
    This would be "www.domain.com" going from the above example. This is needed because some servers run multiple domains, it needs to know which it's looking for.

    You may want to add
    Code:
    Connection: Keep-Alive
    but that's your choice.

    When sending this through Winsock, MAKE SURE to add vbCrLf to the end of the line, I kept leaving this off and it drove me nuts as the server wouldn't respond.

    After sending the above headers send a blank line, e.g., Winsock1.Senddata vbCrLf

    The server will respond with some information about itself and the filesize, then will start sending the file. The server will trigger a disconnect when the file has been sent.

    Disiance
    Last edited by Disiance; Oct 14th, 2004 at 10:52 AM.

  5. #5
    Hyperactive Member Disiance's Avatar
    Join Date
    Sep 2004
    Location
    Denver, CO
    Posts
    439
    Ok, I have made a sample program to help clear things up:
    VB Code:
    1. Private DLFile As String
    2.  
    3. Private Sub Command1_Click()
    4.     Winsock1.RemoteHost = txtServer
    5.     Winsock1.RemotePort = 80
    6.     Winsock1.Connect
    7.     Do Until Winsock1.State = 7
    8.         DoEvents
    9.     Loop
    10.     Winsock1.SendData "GET " & txtFile & " HTTP/1.1" & vbCrLf
    11.     DoEvents
    12.     Winsock1.SendData "Host: " & txtServer & vbCrLf
    13.     DoEvents
    14.     Winsock1.SendData vbCrLf
    15. End Sub
    16.  
    17. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    18.     Dim NewData As String
    19.     Winsock1.GetData NewData
    20.     DLFile = DLFile & NewData
    21. End Sub

    It has a winsock named Winsock1
    Two textboxes, txtServer and txtFile
    The txtServer is the server name e.g. "www.domain.com"
    The txtFile is the file string, e.g. "/folder/file.zip"

    The code will fill variable DLFile with the file (plus headers). I do not recommend downloading into a textbox because textboxes will leave some non-displayable characters out, so I'd download directly into a file, or a variable like shown above. Note the variable can only store about 2 billion characters, so I wouldn't use it for extremely large file downloads, but program updates it should be fine.

    I was wrong in the above post, the server will not close the connection for you, so you'll have to parse the "Content-Length" header it sends and see when it has been reached.

    Disiance

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