Find file size of remote file ie http://www.noteme.com/wokawidget/tickicon.gif
Say there was a file on a web site:
http://www.noteme.com/wokawidget/tickicon.gif
For example.
Is there any VB code that would allow me to find out it's size?
Oh, the SystemFileObject doesn't do remote files, just thought I'd point that out ;)
Woka
Re: Find file size of remote file ie http://www.woof.com/badger.exe
Not via HTTP as far as I'm aware, you'd have to use FTP "ways" or download the file via HTTP and check the size :)
Re: Find file size of remote file ie http://www.woof.com/badger.exe
Hi Wokawidget, actually I have some experiance on this, so I know this for sure:
Connect to the server with winsock, and send a request for "HEAD" header NOT "GET" header
Here's the code, I already tested it
I named the winsock "Sck"
VB Code:
Option Explicit
Private Sub Form_Load()
Sck.RemotePort = 80
Sck.RemoteHost = "www.waka.com"
Sck.Connect
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Sck.Close
End Sub
Private Sub Sck_Connect()
Sck.SendData "HEAD /badger.exe HTTP/1.0" & vbNewLine & _
"Host: www.woof.com" & vbNewLine & _
"User-Agent: blah blah..." & vbNewLine & vbNewLine
End Sub
Private Sub Sck_DataArrival(ByVal bytesTotal As Long)
Dim sData As String, FromPos As Long, ToPos As Long
Sck.GetData sData
FromPos = InStr(1, sData, "Content-Length:", vbTextCompare)
ToPos = InStr(FromPos + 1, sData, vbNewLine)
MsgBox "The file size is: " & Mid(sData, FromPos + 16, ToPos - (FromPos + 16)) & " bytes"
End Sub
PS: The result I got was 717 bytes...
Re: Find file size of remote file ie http://www.woof.com/badger.exe
Nice! I like :D
However, you confused me with:
Code:
"User-Agent: blah blah..."
Are you assuming I know what "Blah Blah" is, or doesn't it matter what I type here?
Also, even though I like the code, and it's very simple, I was looking for something that wasn't asynchronous. However, if it is the only method, then I can code that into my app.
Woka
Re: Find file size of remote file ie http://www.woof.com/badger.exe
The reason I am asking is because of this thread:
http://www.vbforums.com/showthread.php?t=319226
I have created a multi app auto updater.
The one functionality I would like to have is to show the file size of the download before it starts downloading.
Do you have any suggestions that would fit into my code in the other thread?
Woka
Re: Find file size of remote file ie http://www.woof.com/badger.exe
Quote:
Originally Posted by Wokawidget
Nice! I like :D
However, you confused me with:
Code:
"User-Agent: blah blah..."
Are you assuming I know what "Blah Blah" is, or doesn't it matter what I type here?
Woka
It does not matter what you type there, that's the client name, and it can be any name, but some servers don't accept the header (return an error) if the information is missing.
I downloaded and looked at your "multi app auto updater", but I don't understand why you coose to download with the "olelib.tlb". And also, you make the code so complicated, and it makes 2 extra DLLs on top of that... too many DLLs, too many files (classes & modules)...
Generally I would like to make thinkgs simple and to the point, if I would do this, I would do it with Winsock, having the same result, and with aproximatelly the same ammount of code, much less files & one DLL or OCX (wich I would prefer more, for something like this)
Re: Find file size of remote file ie http://www.woof.com/badger.exe
How would you download a file using winsock?
WOka
Re: Find file size of remote file ie http://www.woof.com/badger.exe
All those classes make the code simpler.
And they are DLL's for a reason. You can use the vbDownload and vbAutoUpdater DLL's in any of your apps. No replicating code. Perfect sense if you ask me.
The olelib.tlb is my only draw back, but if you show me a way I can do this with winsock then that would please me...:D
Woka
Re: Find file size of remote file ie http://www.woof.com/badger.exe
Quote:
Originally Posted by Wokawidget
The olelib.tlb is my only draw back, but if you show me a way I can do this with winsock then that would please me...:D
Woka
Well, actually it's quite simple...
Change the header for the code I pasted earlier, from "HEAD..." to "GET..."
That way, the server will send the header (same as when you send the HEAD command"), then 2 new lines, and then the file data.
So, in the first DataArrival event look for the 2 new lines, anything before that, is the header that you want to parse to get the file size (or any other info that might be usefull about the server...), and anything after the 2 new lines, save in the file... this way, you can also make a progress bar, because you get the file size first, then the file data...
If you want, I can make a short example of what I just mentioned.
Re: Find file size of remote file ie http://www.woof.com/badger.exe
If you wouldn't mind. That would be great.
Wonder what the performance difference would be.
Woka
Re: Find file size of remote file ie http://www.woof.com/badger.exe
Here's the modified code.
It downloads the file in the App.Path directory
VB Code:
Option Explicit
Private FileLength As Long, FileNum As Integer
Private Sub Form_Load()
FileNum = FreeFile
Open App.Path & "\badger.exe" For Binary Access Write Lock Write As FileNum
Sck.RemotePort = 80
Sck.RemoteHost = "www.waka.com"
Sck.Connect
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Sck.Close
End Sub
Private Sub Sck_Connect()
Sck.SendData "GET /badger.exe HTTP/1.0" & vbNewLine & _
"Host: www.woof.com" & vbNewLine & _
"User-Agent: blah blah..." & vbNewLine & vbNewLine
End Sub
Private Sub Sck_DataArrival(ByVal bytesTotal As Long)
Dim sData As String, FromPos As Long, ToPos As Long
Sck.GetData sData
If FileLength = 0 Then
Debug.Print sData
If sData Like "HTTP/1.# 200*" Then
FromPos = InStr(1, sData, "Content-Length:", vbTextCompare)
ToPos = InStr(FromPos + 1, sData, vbNewLine)
FileLength = Val(Mid(sData, FromPos + 16, ToPos - (FromPos + 16)))
ToPos = InStr(1, sData, vbNewLine & vbNewLine)
sData = Mid(sData, ToPos + 4)
Else
' some kind of error from the server, check the header (usually file not found)
End If
End If
If FileLength > 0 And Len(sData) > 0 Then
Put FileNum, , sData
If LOF(FileNum) >= FileLength Then
MsgBox "DONE"
Close FileNum
FileNum = 0
FileLength = 0
End If
End If
End Sub
Re: Find file size of remote file ie http://www.woof.com/badger.exe
Nice code.
But out of interest, what's wrong with DownloadURL API that I was using?
It does everything that the winsock does, but automatically. Also, it comes with other benefits, like caching the file.
So, you have to include 2 tlb files in your app...and? When dealing with large apps that are very complex then you will have many many many DLLs associated with your app. Some your have written yourself, and some that are 3rd party.
Splitting an app up into different components, whether that be internal classes, or DLL, benefits you and the development of the application.
There is no way everything should be placed in one exe. This makes for bad app design.
Woka
Re: Find file size of remote file ie http://www.woof.com/badger.exe
Am trying to get your code working, I have:
Code:
Public Sub GetFileInformation()
Dim strData As String
On Error GoTo ErrHandler
mlngDownloadType = TYPE_INFO
If IsConnected Then
strData = "HEAD " & mstrRemoteFile & " HTTP/1.0" & vbNewLine
strData = strData & "Host: www.Woka.com" & vbNewLine
strData = strData & "User-Agent: blah blah..." & vbNewLine & vbNewLine
mobjSocket.SendData strData
Else
Connect
End If
Exit Sub
ErrHandler:
Disconnect
Err.Raise Err.Number, Err.Source, Err.Description
End Sub
Where:
Code:
mstrRemoteFile = "/Wokawidget/TickIcon.gif"
and
Code:
mobjSocket.RemoteHost = "www.noteme.com"
mobjSocket.RemotePort = 80
but I always get "404 File Not Found" as the returned data.
It exists, use IE to browse to it. NoteMe is hosting it for me.
The sent data was:
Quote:
HEAD /wokawidget/TickIcon.gif HTTP/1.0
Host:
www.Woka.com
User-Agent: blah blah...
and the returned data is:
Quote:
HTTP/1.1 404 Not Found
Date: Wed, 19 Jan 2005 02:15:14 GMT
Server: Apache/1.3.33 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 PHP/4.3.10 FrontPage/5.0.2.2635 mod_ssl/2.8.22 OpenSSL/0.9.7a
Connection: close
Content-Type: text/html; charset=iso-8859-1
Does it have anything to do with HTTP/1.1 and HTTP/1.0.
Also, "Connection: close", does this mean there is no connection?
The GetFileInformation function was fired on the OnConnect event from winsock.
Any ideas why it can't see it?
Woka
Re: Find file size of remote file ie http://www.woof.com/badger.exe
Are you sure the path is correct ?
Maybe:
mstrRemoteFile = "/TickIcon.gif" ?
If you type the link in IE, what is the COMPLETE link ? http://... ?
Quote:
Also, "Connection: close", does this mean there is no connection?
It basically means that the server will close the connection once it's done sending all data. Some servers keep the connection open because you might want to send another request, and so on...
Re: Find file size of remote file ie http://www.woof.com/badger.exe
Quote:
Maybe:
mstrRemoteFile = "/TickIcon.gif" ?
Nope...it's in the Wokawidget virtual directory. Full path:
http://www.noteme.com/Wokawidget/TickIcon.gif
Woka
Re: Find file size of remote file ie http://www.noteme.com/wokawidget/tickicon.gif
I colored in Red the changes you have to make.
This code worked for me...
VB Code:
Option Explicit
Private FileLength As Long, FileNum As Integer
Private Sub Form_Load()
FileNum = FreeFile
Open App.Path & [COLOR=Red]"\TickIcon.gif"[/COLOR] For Binary Access Write Lock Write As FileNum
Sck.RemotePort = 80
Sck.RemoteHost = [COLOR=Red]"www.noteme.com"[/COLOR]
Sck.Connect
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Sck.Close
End Sub
Private Sub Sck_Connect()
Sck.SendData "GET [COLOR=Red]/Wokawidget/TickIcon.gif[/COLOR] HTTP/1.0" & vbNewLine & _
"Host: [COLOR=Red]www.noteme.com"[/COLOR] & vbNewLine & _
"User-Agent: blah blah..." & vbNewLine & vbNewLine
End Sub
Private Sub Sck_DataArrival(ByVal bytesTotal As Long)
Dim sData As String, FromPos As Long, ToPos As Long
Sck.GetData sData
If FileLength = 0 Then
Debug.Print sData
If sData Like "HTTP/1.# 200*" Then
FromPos = InStr(1, sData, "Content-Length:", vbTextCompare)
ToPos = InStr(FromPos + 1, sData, vbNewLine)
FileLength = Val(Mid(sData, FromPos + 16, ToPos - (FromPos + 16)))
ToPos = InStr(1, sData, vbNewLine & vbNewLine)
sData = Mid(sData, ToPos + 4)
Else
' some kind of error from the server, check the header (usually file not found)
End If
End If
If FileLength > 0 And Len(sData) > 0 Then
Put FileNum, , sData
If LOF(FileNum) >= FileLength Then
MsgBox "DONE"
Close FileNum
FileNum = 0
FileLength = 0
End If
End If
End Sub
Re: Find file size of remote file ie http://www.noteme.com/wokawidget/tickicon.gif
Think I just realised what was going wrong :(
The following works:
/Wokawidget/TickIcon.gif
This doesn't:
/Wokawidget/tickicon.gif
Stupid apache web server with case sensitive settings :(
Cheers for your help.
Woka
Re: Find file size of remote file ie http://www.noteme.com/wokawidget/tickicon.gif
Balls! :(
Not only is it case sensetive, but:
Code:
"Host: www.noteme.com" & vbNewLine & _
That MUST be the host remote server iun there.
I had www.woof.com, which is another reason it failed :(
Got it working now...WOOOHOOOOOOOOOOO!
Thank you.
Woka