Avatar billede sunep Nybegynder
23. juli 2002 - 22:31 Der er 2 kommentarer og
1 løsning

downloade en fil fra nettet

Findes der noget kode så man kan lave et program der kan downloade en fil fra nettet.. uden at man skal igennem dialog boksen først.. man må osse godt måle hvad man dl med... men det er ikke nødvendigt
Avatar billede kantoraza Nybegynder
23. juli 2002 - 23:31 #1
Ja da.

--Kopier denne kode til visual basic's generel--

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
Public Function DownloadFile(URL As String, LocalFilename As String)
Dim lngRetVal As Long
  lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
End Function
Private Sub Command1_Click()
' Source: http://www.friserverplads.dk/basicpage/fil.txt


Dim strArray
Dim strText
  DownloadFile "http://www.minside.dk/noget.txt", "c:\windows\temp\soucedownload.txt" ' en linje
  Open "c:\windows\temp\soucedownload.txt" For Input As #1
    Do Until EOF(1)
      Input #1, strText
      List1.AddItem strText
    Loop
  Close #1


End Sub


KantoRaZa·NorthCode
Avatar billede larsen Nybegynder
24. juli 2002 - 10:57 #2
Du kan kigge på PlanetSourceCode. Der ligger der mange under VB.

F.eks :
AutoUpdate (http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=13413&lngWId=1)

Du kan prøve at søge på : WinSock & WinInet
Avatar billede sjh Nybegynder
29. juli 2002 - 15:29 #3
'----------------------------- Form1 -----------------------------
Option Explicit

Private Declare Sub InternetCloseHandle Lib "wininet.dll" ( _
    ByVal hInet As Long)

Private Declare Function InternetOpenA Lib "wininet.dll" ( _
    ByVal sAgent As String, ByVal lAccessType As Long, _
    ByVal sProxyName As String, ByVal sProxyBypass As String, _
    ByVal lFlags As Long) As Long

Private Declare Function InternetOpenUrlA Lib "wininet.dll" ( _
    ByVal hOpen As Long, ByVal sUrl As String, _
    ByVal sHeaders As String, ByVal lLength As Long, _
    ByVal lFlags As Long, ByVal lContext As Long) As Long

Private Declare Sub InternetReadFile Lib "wininet.dll" ( _
    ByVal hFile As Long, ByVal sBuffer As String, _
    ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long)

Public Enum InternetOpenType
  IOTPreconfig = 0
  IOTDirect = 1
  IOTProxy = 3
End Enum

Private Const INET_RELOAD = &H80000000
Private Cancel_Download As Boolean

Public Function DownloadBytes(Bytes As Long)
  Me.Caption = "Download: " & Int(Bytes / 1024) & " KBytes"
End Function

Public Function DownloadFile(ByVal URL As String, FileName As String, Optional ByVal OpenType As InternetOpenType = IOTPreconfig)
Dim hInet As Long
Dim hURL As Long
Dim Buffer As String * 2048
Dim Bytes As Long
Dim TotalBytes As Long
  Cancel_Download = False
  hInet = InternetOpenA("VB-Program", OpenType, vbNullString, vbNullString, 0)
  hURL = InternetOpenUrlA(hInet, URL, vbNullString, 0, INET_RELOAD, 0)
    Open FileName For Binary As #1
      Do
        InternetReadFile hURL, Buffer, Len(Buffer), Bytes
          Put #1, , Left$(Buffer, Bytes)
            TotalBytes = TotalBytes + Len(Left$(Buffer, Bytes))
          DoEvents
        Call DownloadBytes(TotalBytes)
      Loop Until Bytes = 0 Or Cancel_Download = True
    Close #1
  InternetCloseHandle hURL
  InternetCloseHandle hInet
End Function

Private Sub Command1_Click()
  DownloadFile "http://www.navn.dk/file.exe", "C:\file.exe"
End Sub

Private Sub Command2_Click()
  Cancel_Download = True
End Sub

Private Sub Form_Load()
  Command1.Caption = "Start"
  Command2.Caption = "Annuller"
End Sub
'----------------------------- Form1 -----------------------------
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester