|
-
Oct 14th, 2004, 03:31 AM
#1
Thread Starter
PowerPoster
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:
Option Explicit
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
Private Sub Form_Load()
Dim url As String
Dim localFileName As String
Dim errcode As Long
url = "http://www.mysite.com/myfile.txt"
localFileName = App.Path & "\myfile.dat"
errcode = URLDownloadToFile(0, url, localFileName, 0, 0)
End Sub
-
Oct 14th, 2004, 09:40 AM
#2
Hyperactive Member
Don't use the URLDownloadToFile API *gasp!*
Plugin a winsock control instead, connect on port 80, send the nessicary headers and download.
Disiance
-
Oct 14th, 2004, 10:05 AM
#3
Frenzied Member
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.
-
Oct 14th, 2004, 10:44 AM
#4
Hyperactive Member
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
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.
-
Oct 14th, 2004, 11:07 AM
#5
Hyperactive Member
Ok, I have made a sample program to help clear things up:
VB Code:
Private DLFile As String
Private Sub Command1_Click()
Winsock1.RemoteHost = txtServer
Winsock1.RemotePort = 80
Winsock1.Connect
Do Until Winsock1.State = 7
DoEvents
Loop
Winsock1.SendData "GET " & txtFile & " HTTP/1.1" & vbCrLf
DoEvents
Winsock1.SendData "Host: " & txtServer & vbCrLf
DoEvents
Winsock1.SendData vbCrLf
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim NewData As String
Winsock1.GetData NewData
DLFile = DLFile & NewData
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|