dcsimg
Results 1 to 4 of 4

Thread: How to get the FileName of a URL ?

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2018
    Posts
    1

    How to get the FileName of a URL ?

    Does anyone have a VB6.0 example of how to get the filename of a URL when that filename is unknown, possibly by reading the header / Content-Disposition value / MIME ?

    The fictional example is below:

    "https://abc.com/program.cfm?fileid=" & XXXXXXX"

    I'm running on a Windows 7 64 bit PC.

    I have code below that downloads the file, but I am hardcoding the filename to test.pdf and want to use the actual filename. Appreciate any assistance, as I've been scouring the net for hours.

    Dim httpString As String
    httpString = "https://www.abc.com/program.cfm?value=" & value

    CopyURLToFile httpString, "C:\Users\Username\Desktop\MyDownload\test.pdf"


    Sub CopyURLToFile(ByVal URL As String, ByVal FileName As String)

    Dim hInternetSession As Long
    Dim hUrl As Long
    Dim FileNum As Integer
    Dim ok As Boolean
    Dim NumberOfBytesRead As Long
    Dim Buffer As String
    Dim fileIsOpen As Boolean

    On Error GoTo ErrorHandler

    ' check obvious syntax errors
    If Len(URL) = 0 Or Len(FileName) = 0 Then Err.Raise 5

    ' open an Internet session, and retrieve its handle
    hInternetSession = InternetOpen(App.EXEName, INTERNET_OPEN_TYPE_PRECONFIG, _
    vbNullString, vbNullString, 0)
    If hInternetSession = 0 Then Err.Raise vbObjectError + 1000, , _
    "An error occurred calling InternetOpen function"

    ' open the file and retrieve its handle
    hUrl = InternetOpenUrl(hInternetSession, URL, vbNullString, 0, _
    INTERNET_FLAG_EXISTING_CONNECT, 0)
    If hUrl = 0 Then Err.Raise vbObjectError + 1000, , _
    "An error occurred calling InternetOpenUrl function"

    ' ensure that there is no local file
    On Error Resume Next
    Kill FileName

    On Error GoTo ErrorHandler

    ' open the local file
    FileNum = FreeFile
    Open FileName For Binary As FileNum
    fileIsOpen = True

    ' prepare the receiving buffer
    Buffer = Space(4096)

    Do
    ' read a chunk of the file - returns True if no error
    ok = InternetReadFile(hUrl, Buffer, Len(Buffer), NumberOfBytesRead)

    ' exit if error or no more data
    If NumberOfBytesRead = 0 Or Not ok Then Exit Do

    ' save the data to the local file
    Put #FileNum, , Left$(Buffer, NumberOfBytesRead)
    Loop

    ' flow into the error handler

    ErrorHandler:
    ' close the local file, if necessary
    If fileIsOpen Then Close #FileNum
    ' close internet handles, if necessary
    If hUrl Then InternetCloseHandle hUrl
    If hInternetSession Then InternetCloseHandle hInternetSession

    ' report the error to the client, if there is one
    If Err Then Err.Raise Err.Number, , Err.Description
    End Sub

  2. #2

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,720

    Re: How to get the FileName of a URL ?

    Changing the URL doesn't fool anybody. it's the same as the old "I just want to crack my own passwords" scam.

  4. #4
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,351

    Re: How to get the FileName of a URL ?

    There's a million different scenarios where it's perfectly ok to download a file that's in a URL. In fact I was probably even mistaken here, since this wouldn't bypass their DRM if they didn't want it saved.

    Not to mention the only time I've looked into cracking pws it was because I forgot one of my own. Everybody forgets passwords.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width