Avatar billede dth Nybegynder
27. januar 2002 - 14:00 Der er 2 kommentarer og
2 løsninger

Hent fil

Jeg skal have mit program til at hente en fil fra nettet..
Den skal lægge den i app.path, åbne den, hente teksten ind og slette filen...

Hvordan kan jeg gøre det??

/trier :-)
Avatar billede sjh Nybegynder
27. januar 2002 - 15:48 #1
Download det uden at gemme det på disk

'------------------------------------------- Form1 -------------------------------------------
Private Declare Function InternetOpen Lib "wininet" Alias "InternetOpenA" ( _
                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 InternetOpenUrl Lib "wininet" 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 InternetReadFile Lib "wininet" ( _
                ByVal hFile As Long, ByVal sBuffer As String, _
                ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer

Private Declare Function InternetCloseHandle Lib "wininet" ( _
                ByVal hInet As Long) As Integer

Dim Cancel_Download As Long

Function DownloadString(UrlFile As String) As String
Dim UrlInternetOpen As Long
Dim UrlInternetOpenUrl As Long
Dim UrlBuffer As String * 2048
Dim UrlBytes As Long
Dim UrlComplete As String

Cancel_Download = 0

  UrlInternetOpen = InternetOpen("Download String", 1, vbNullString, vbNullString, 0)
  UrlInternetOpenUrl = InternetOpenUrl(UrlInternetOpen, UrlFile, vbNullString, 0, &H80000000, 0)
    Do
      InternetReadFile UrlInternetOpenUrl, UrlBuffer, Len(UrlBuffer), UrlBytes
        If UrlBytes = 0 Or Cancel_Download = 1 Then Exit Do 'Cancel_Download
          UrlComplete = UrlComplete & Left$(UrlBuffer, UrlBytes)
        DownloadBytes Len(UrlComplete)  'Function DownloadBytes
      DoEvents
    Loop
  InternetCloseHandle UrlInternetOpenUrl
  InternetCloseHandle UrlInternetOpen

DownloadString = UrlComplete
End Function

Function DownloadBytes(Bytes As Long)
  Me.Caption = "Download: " & Bytes & " Bytes"
End Function

Private Sub Command1_Click()
  RichTextBox1.Text = DownloadString("http://www.eksperten.dk/spm/164352")
End Sub

Private Sub Command2_Click()
  Cancel_Download = 1
End Sub

Private Sub Form_Load()
  Command1.Caption = "Download"
  Command2.Caption = "Cancel"
  RichTextBox1.Text = ""
End Sub
'------------------------------------------- Form1 -------------------------------------------
Avatar billede dr.truti Nybegynder
27. januar 2002 - 21:15 #2
Denne kode minder lidt om sjh's kode men denne er lidt mindre og gør stort set det samme....

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

Function DownloadFile(URL As String, LocalFilename As String) As Boolean
Dim lngRetVal As Long
lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then DownloadFile = True
End Function

Private Sub Form_Load()
DownloadFile "http://home1.stofanet.dk/truti/index.htm", "udvikleren.htm"
End Sub

Håber du kan bruge dette !!!

Truti
ICQ: 72755522
Mail: maac@mail1.stofanet.
HP: http://home1.stofanet.dk/truti/
Avatar billede dr.truti Nybegynder
27. januar 2002 - 21:15 #3
Svar!
Avatar billede dr.truti Nybegynder
05. februar 2002 - 21:54 #4
På din form skal du have en Knap og en Text boks!

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

Function DownloadFile(URL As String, LocalFilename As String) As Boolean
Dim lngRetVal As Long
lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then DownloadFile = True
End Function

Private Sub Command1_Click()
Dim download_url As String

Text1.Text = download_url
DownloadFile "" & download_url & "", "udvikleren.htm"
End Sub

Truti
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

IT-JOB

Capgemini Danmark A/S

Java Engineer

Netcompany A/S

IT Consultant

Rambøll Management Consulting

Senior AI Engineer