Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1449

Download file from WEB in a few lines

$
0
0
This very short code can download a file from an URL :
Ex :
Call DownloadFile("https://www.vbforums.com/attachment.php?attachmentid=179280&d=1605528528", "D:\test.zip")

Code:

Public Function DownloadFile(sURL As String, sLocalFilename As String, Optional pUserName As String, Optional pPassWord As String) As Boolean
  ' #VBIDEUtils#************************************************************
  ' * Author          :
  ' * Web Site        :
  ' * E-Mail          :
  ' * Date            : 01/02/2006
  ' * Time            : 12:35
  ' * Module Name      : Lib_Module
  ' * Module Filename  : Lib.bas
  ' * Procedure Name  : DownloadFile
  ' * Purpose          :
  ' * Parameters      :
  ' *                    sURL As String
  ' *                    sLocalFilename As String
  ' *                    Optional pUserName As String
  ' *                    Optional pPassWord As String
  ' * Purpose          :
  ' **********************************************************************
  ' * Comments        :
  ' *
  ' *
  ' * Example          :
  ' *
  ' * See Also        :
  ' *
  ' * History          :
  ' *
  ' *
  ' **********************************************************************

  ' #VBIDEUtilsERROR#
  On Error GoTo ERROR_DownloadFile

  Dim oHTTP            As Object ' MSXML2.XMLHTTP
  Dim nFile            As Integer
  Dim oByte()          As Byte

  Set oHTTP = CreateObject("MSXML2.XMLHTTP")
  oHTTP.Open "POST", sURL, False, pUserName, pPassWord
  oHTTP.Send
  If oHTTP.Status = 200 Then
      oByte = oHTTP.responseBody

      If Dir(sLocalFilename) <> vbNullString Then Kill sLocalFilename
      nFile = FreeFile
      Open sLocalFilename For Binary As #nFile
      Put #nFile, , oByte
      Close #nFile
  End If

EXIT_DownloadFile:
  On Error Resume Next

  Exit Function

  ' #VBIDEUtilsERROR#
ERROR_DownloadFile:
  Resume EXIT_DownloadFile

End Function


Viewing all articles
Browse latest Browse all 1449

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>