Results 1 to 6 of 6

Thread: Download from web [RESOLVED]

  1. #1

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Download from web [RESOLVED]

    HI all,

    I have a webpath say: www.webserver.com/getfile.asp

    How to download the resulting htm file thru vb.net?

    in vb6 i used to do the following:

    VB Code:
    1. 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
    2.  
    3.     Public Function DownloadFile(ByVal URL As String, ByVal LocalFilename As String) As Boolean
    4.         Dim lngRetVal As Long
    5.         lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
    6.         If lngRetVal = 0 Then DownloadFile = True
    7.     End Function


    pls guide
    Last edited by pvbangera; Apr 2nd, 2004 at 10:21 AM.

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    VB Code:
    1. Dim myClient As New System.Net.WebClient
    2. myClient.DownloadFile("http://www.google.com","C:\mydump.txt")

    Good enough?

  3. #3

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    I tried that. But it gives me following error:

    'The underlying connection was closed: The remote name could not be resolved.'



    If I copy and paste the same URL in IE then the page is opened without any error.

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, I don't know exactly what you wanted out of the function, here's the same thing kind of with error-catching, and intretval will return 0 if the resource was not found, resulting in a false return from the function.

    VB Code:
    1. Public Function DownloadFile(ByVal URL As String, ByVal LocalFileName As String) As Boolean
    2.  
    3.         Dim myClient As New System.Net.WebClient
    4.         Dim result As Boolean = False
    5.         Dim S As System.IO.FileStream
    6.         Dim myBuffer As Byte()
    7.         Try
    8.             myBuffer = myClient.DownloadData(URL)
    9.             S = New IO.FileStream(LocalFileName, IO.FileMode.OpenOrCreate)
    10.             S.Write(myBuffer, 0, myBuffer.Length)
    11.             S.Flush()
    12.         Catch
    13.  
    14.         Finally
    15.             If Not S Is Nothing Then S.Close()
    16.             myClient.Dispose()
    17.  
    18.         End Try
    19.         If Not myBuffer Is Nothing Then result = True
    20.         Return result
    21.     End Function

    If that works for you, please append resolved to your message title.
    Last edited by nemaroller; Apr 2nd, 2004 at 09:51 AM.

  5. #5

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    its working fine. thanx a lot. u r a darling

  6. #6

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    The stuff is working fine. But it takes lots of time to download.

    Is there any other way out?

Posting Permissions

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



Click Here to Expand Forum to Full Width