Friday, September 22, 2017

ການດາວໂຫຼດໄຟລ ຈາກ ອິນເຕີເນັດ (VB6 Lesson 19)

ໃນບົດນີ້ ແມ່ນການຂຽນໂປຣແກຣມໃຫ້ດາວໂຫຼດໄຟລ ຈາກອິນເຕີເນັດ
ຕົວຢ່າງ 1
ເປັນ Function ສຳລັບດາວໂຫຼດໄຟລ

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 Const ERROR_SUCCESS As Long = 0
Private Const BINDF_GETNEWESTVERSION As Long = &H10
Private Const INTERNET_FLAG_RELOAD As Long = &H80000000
Public Function DownloadFile(sSourceUrl As String, _
                             sLocalFile As String) As Boolean
  //'Download the file. BINDF_GETNEWESTVERSION forces 
  //'the API to download from the specified source. 
  //'Passing 0& as dwReserved causes the locally-cached 
  //'copy to be downloaded, if available. If the API 
  //'returns ERROR_SUCCESS (0), DownloadFile returns True.
   DownloadFile = URLDownloadToFile(0&, _
                                    sSourceUrl, _
                                    sLocalFile, _
                                    BINDF_GETNEWESTVERSION, _
                                    0&) = ERROR_SUCCESS
End Function

ຕົວຢ່າງ 2

ເປັນ Sub Procedure

Sub DownloadFile(url, path)
   Dim objReq
   Dim objStream
   Set objReq = CreateObject("MSXML2.XMLHTTP")
   objReq.Open "GET", url, False
   objReq.send
   If objReq.Status = 200 Then
       Set objStream = CreateObject("ADODB.Stream")
       objStream.Open
       objStream.Type = 1
       objStream.Write objReq.ResponseBody
       objStream.Position = 0
       objStream.SaveToFile path, 2
       objStream.Close
       Set objStream = Nothing
   End If
   Set objReq = Nothing
End Sub

ຕົວຢ່າງ 3

Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Const INTERNET_FLAG_EXISTING_CONNECT = &H20000000
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
    (ByVal lpszAgent As String, ByVal dwAccessType As Long, _
    ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, _
    ByVal dwFlags As Long) As Long
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias _
    "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As String, _
    ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, _
    ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As _
    Long) As Integer
Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As _
    Long, ByVal lpBuffer As String, ByVal dwNumberOfBytesToRead As Long, _
    lNumberOfBytesRead As Long) As Integer
' Download a file from Internet and save it to a local file
'
' it works with HTTP and FTP, but you must explicitly include
' the protocol name in the URL, as in
'    CopyURLToFile "http://www.vb2themax.com/default.asp", "C:\vb2themax.htm"
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

Subscribe

  • RSS Atom

ອອນລາຍ: 1 | ມື້ນີ້: 13 | ວານນີ້: 25 | ທິດນີ້: 93 | ເດືອນນີ້: 872 | ປີນີ້: 11832 | ລວມ: 78935