locked
Downloading Files RRS feed

  • Question

  • Hi:

    i want to use

     

    Dim myWebClient As New System.Net.WebClient

    myWebClient.DownloadFile(,)

     

    for Downloading files in my program.

    In some webpages like

    http://www.powershares.com/products/holdings.aspx?ticker=PRFZ

     

    ( as you can see we couldn't find out about Url with overing mouse to Download Link)

    i think this Javascript:__dopostback(,) function, concat some Strings for producing Target URL, i don't know what this function do at this page.

     is there any way for getting Target Url which i can use it in "myWebClient.DownloadFile(,)" function?

    is there another way for downloading these files?

     

    Thank you in Advance

    Farsmajid

     

    Sunday, October 21, 2007 9:56 AM

Answers

  • farsmajid,

     

    According to your question on download files with System.Net.WebClient.DownloadFile method, I would like to provide you the suggestions as follows:

     

    1. The DownloadFile method downloads to a local file data from the URI specified by in the address parameter. This method blocks while downloading the resource. To download a resource and continue executing while waiting for the server's response, use one of the DownloadFileAsync methods.

     

    Please try the following code snippet in WebClient.DownloadFile Method (String, String) from MSDN that downloads a file from http://www.contoso.com to the local hard drive

     

    Code Block

            Dim remoteUri As String = "http://www.contoso.com/library/homepage/images/"

            Dim fileName As String = "ms-banner.gif"

            Dim myStringWebResource As String = Nothing

            ' Create a new WebClient instance.

            Dim myWebClient As New WebClient()

            ' Concatenate the domain with the Web resource filename. Because DownloadFile

            'requires a fully qualified resource name, concatenate the domain with the Web resource file name.

            myStringWebResource = remoteUri + fileName

            Console.WriteLine("Downloading File ""{0}"" from ""{1}"" ......." + ControlChars.Cr + ControlChars.Cr, fileName, myStringWebResource)

            ' The DownloadFile() method downloads the Web resource and saves it into the current file-system folder.

            myWebClient.DownloadFile(myStringWebResource, fileName)

            Console.WriteLine("Successfully Downloaded file ""{0}"" from ""{1}""", fileName, myStringWebResource)

            Console.WriteLine((ControlChars.Cr + "Downloaded file saved in the following file system folder:" + ControlChars.Cr + ControlChars.Tab + Application.StartupPath))

     

     

     

    2. The following thread with the example provided by Indian Ocean can help you further on the problem:

     

    VB.Net 2.0 File download

     

    Code Block

        Private Function DownloadFileFromHttpOrFtp(ByVal remotefilepath As String, ByVal filepathandname2savewith As String, ByVal issecure As Boolean, ByVal username As String, ByVal password As String) As String

            Dim client As System.Net.WebClient = New System.Net.WebClient() 'You can have this at class level so dont need to instantiate for each request

            If issecure = True Then

                'Set username/pass required for a url access

                Dim myCred As System.Net.NetworkCredential = New System.Net.NetworkCredential(username, password)

                client.Credentials = myCred

            End If

            Dim data As Byte() = client.DownloadData(remotefilepath)

            Dim downloadedfiledata As String = System.Text.Encoding.ASCII.GetString(data)

            Return downloadedfiledata

        End Function

     

     

    Hope that can help you.
    Wednesday, October 24, 2007 5:44 AM
  • farsmajid,

     

    Since there is no target URL to show the specific remote file location, how would you know the file and download with DownloadFile method? Actually I am quite understand your question exactly.

     

    Have you tried to use My.Computer.Network.DownloadFile Method and Microsoft.VisualBasic.Devices.Network.DownloadFile Method in .NET Framework 2.0?

     

    If you there is already URL and you would like to automate the download process, I suggest you to send the keyboard and mouse click in your source code as the following article:

     

    How to: Simulate Mouse and Keyboard Events in Code

     

    If it is not the case, please reply with the further question.

    Wednesday, October 24, 2007 7:57 AM

All replies

  • farsmajid,

     

    According to your question on download files with System.Net.WebClient.DownloadFile method, I would like to provide you the suggestions as follows:

     

    1. The DownloadFile method downloads to a local file data from the URI specified by in the address parameter. This method blocks while downloading the resource. To download a resource and continue executing while waiting for the server's response, use one of the DownloadFileAsync methods.

     

    Please try the following code snippet in WebClient.DownloadFile Method (String, String) from MSDN that downloads a file from http://www.contoso.com to the local hard drive

     

    Code Block

            Dim remoteUri As String = "http://www.contoso.com/library/homepage/images/"

            Dim fileName As String = "ms-banner.gif"

            Dim myStringWebResource As String = Nothing

            ' Create a new WebClient instance.

            Dim myWebClient As New WebClient()

            ' Concatenate the domain with the Web resource filename. Because DownloadFile

            'requires a fully qualified resource name, concatenate the domain with the Web resource file name.

            myStringWebResource = remoteUri + fileName

            Console.WriteLine("Downloading File ""{0}"" from ""{1}"" ......." + ControlChars.Cr + ControlChars.Cr, fileName, myStringWebResource)

            ' The DownloadFile() method downloads the Web resource and saves it into the current file-system folder.

            myWebClient.DownloadFile(myStringWebResource, fileName)

            Console.WriteLine("Successfully Downloaded file ""{0}"" from ""{1}""", fileName, myStringWebResource)

            Console.WriteLine((ControlChars.Cr + "Downloaded file saved in the following file system folder:" + ControlChars.Cr + ControlChars.Tab + Application.StartupPath))

     

     

     

    2. The following thread with the example provided by Indian Ocean can help you further on the problem:

     

    VB.Net 2.0 File download

     

    Code Block

        Private Function DownloadFileFromHttpOrFtp(ByVal remotefilepath As String, ByVal filepathandname2savewith As String, ByVal issecure As Boolean, ByVal username As String, ByVal password As String) As String

            Dim client As System.Net.WebClient = New System.Net.WebClient() 'You can have this at class level so dont need to instantiate for each request

            If issecure = True Then

                'Set username/pass required for a url access

                Dim myCred As System.Net.NetworkCredential = New System.Net.NetworkCredential(username, password)

                client.Credentials = myCred

            End If

            Dim data As Byte() = client.DownloadData(remotefilepath)

            Dim downloadedfiledata As String = System.Text.Encoding.ASCII.GetString(data)

            Return downloadedfiledata

        End Function

     

     

    Hope that can help you.
    Wednesday, October 24, 2007 5:44 AM
  • Hi:

    Thank you so much for your consideration.

    My problem is that I want to download a file that I don’t have its URL completely while downloading files requires a fully qualified resource name.

    As you can see, we manually can download from this url by clicking at download: (http://www.powershares.com/products/holdings.aspx?ticker=PRFZ)

    My purpose is automating of this process.

     

    Thank you again for your Help

    Farsmajid

     

    Wednesday, October 24, 2007 7:12 AM
  • farsmajid,

     

    Since there is no target URL to show the specific remote file location, how would you know the file and download with DownloadFile method? Actually I am quite understand your question exactly.

     

    Have you tried to use My.Computer.Network.DownloadFile Method and Microsoft.VisualBasic.Devices.Network.DownloadFile Method in .NET Framework 2.0?

     

    If you there is already URL and you would like to automate the download process, I suggest you to send the keyboard and mouse click in your source code as the following article:

     

    How to: Simulate Mouse and Keyboard Events in Code

     

    If it is not the case, please reply with the further question.

    Wednesday, October 24, 2007 7:57 AM
  •  

    Hi:

    These 2 methods don’t work properly because both of them need a fully specified URL.

    About simulating Mouse, my problem is to finding the position of Download at webpage, because its change from one URL to another URL.

     

    Thank you for your Helps

    Farsmajid

    Friday, October 26, 2007 6:34 AM