Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Determine a download's file-name from http request?

Reply
 
Old 09-06-2008   #1 (permalink)
ShayneH


 
 

Determine a download's file-name from http request?

Below is a script that downloads a binary file over http.
What I need is a way of obtaining the file name returned by a URL
request that is processed on the server side.
eg. "http://www.downloads.com/files?id=abcdef12345" may return
"archive.zip"
Can the XMLHTTP object reveal the file name "archive.zip" to me?

-----Code------

Private Sub FetchBinaryFile(FileURL, LocalFile)

Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")

objXMLHTTP.open "GET", FileURL, false
objXMLHTTP.send()
If objXMLHTTP.Status = 200 Then
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0 'Set the stream position to the start

Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.FileExists(LocalFile) Then objFSO.DeleteFile LocalFile
Set objFSO = Nothing

objADOStream.SaveToFile LocalFile
objADOStream.Close
Set objADOStream = Nothing
End if
Set objXMLHTTP = Nothing
End Sub

-----Code------

My System SpecsSystem Spec
Old 09-07-2008   #2 (permalink)
Tim Williams


 
 

Re: Determine a download's file-name from http request?

Try looking into

objXMLHTTP.getAllResponseHeaders()

From there you may be able to parse out the suggested filename using
getResponseHeader("headername").


Tim


"ShayneH" <Shayne.Husson@xxxxxx> wrote in message
news:a243b8a3-f9f0-4088-95ef-0dee9b261146@xxxxxx
Quote:

> Below is a script that downloads a binary file over http.
> What I need is a way of obtaining the file name returned by a URL
> request that is processed on the server side.
> eg. "http://www.downloads.com/files?id=abcdef12345" may return
> "archive.zip"
> Can the XMLHTTP object reveal the file name "archive.zip" to me?
>
> -----Code------
>
> Private Sub FetchBinaryFile(FileURL, LocalFile)
>
> Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
>
> objXMLHTTP.open "GET", FileURL, false
> objXMLHTTP.send()
> If objXMLHTTP.Status = 200 Then
> Set objADOStream = CreateObject("ADODB.Stream")
> objADOStream.Open
> objADOStream.Type = 1 'adTypeBinary
> objADOStream.Write objXMLHTTP.ResponseBody
> objADOStream.Position = 0 'Set the stream position to the start
>
> Set objFSO = Createobject("Scripting.FileSystemObject")
> If objFSO.FileExists(LocalFile) Then objFSO.DeleteFile LocalFile
> Set objFSO = Nothing
>
> objADOStream.SaveToFile LocalFile
> objADOStream.Close
> Set objADOStream = Nothing
> End if
> Set objXMLHTTP = Nothing
> End Sub
>
> -----Code------

My System SpecsSystem Spec
Old 09-07-2008   #3 (permalink)
Anthony Jones


 
 

Re: Determine a download's file-name from http request?

"Tim Williams" <timjwilliams at gmail dot com> wrote in message
news:umhM4$JEJHA.4744@xxxxxx
Quote:

> Try looking into
>
> objXMLHTTP.getAllResponseHeaders()
>
> From there you may be able to parse out the suggested filename using
> getResponseHeader("headername").
>
The header name required is: Content-Disposition.

It will contain filename="archive.zip" and may also contain the attachment
keyword. Hence it still needs parsing:-

sFileName = GetFileName(objXMLHTTP.getResponseHeader("Content-Disposition"))

Function GetFileName(rsHeader)

Dim rgx : Set rgx = New RegExp
rgx.Pattern = "filename=(?:""([^""]+)""|([^;]+);?)"
rgx.IgnoreCase = True

GetFileName = "Unknown.dat"

Dim oMatch
For Each oMatch in rgx.Execute(rsHeader)
GetFileName = oMatch.SubMatches(0)
If GetFileName = "" Then GetFileName = oMatch.SubMatches(1)
Exit For
Next

End Function
--
Anthony Jones - MVP ASP/ASP.NET

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
HTTP error 400, bad request (invalid verb) Vista mail
HTTP 400 Bad Request Vista General
3 second lag when making http request to localhost Vista networking & sharing
Http request problem PowerShell
http request and host header PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46