Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
How to pick a file on a web site and store it not open it.

Answered How to pick a file on a web site and store it not open it.

  • Wednesday, September 19, 2012 6:49 PM
     
     

    I have VB application that needs access to a txt file. The text files will be listed on my customers web site. When I navigate to the list of text files and I click on one it opens in the browser window. I need to either automatically store the file on the local drive or have access to it directly from the web page. I dont want to see it on the screen however. It's probably a simple task, but I'm new to all this MS library stuff.

    Thanks

    My.Computer.Network.DownloadFile _
      

All Replies

  • Wednesday, September 19, 2012 7:00 PM
     
     

    You go to the website and click on the text file you want to access but you are not prompted if you want to download the text file or open it in the webBrowser window? Can you provide the URL so I can test some code against it?


    You've taught me everything I know but not everything you know.

  • Wednesday, September 19, 2012 7:04 PM
    Moderator
     
      Has Code

    If you will always know the name of the file you need to download, then you can use code like this:

            Dim myURL = "http://www.somesite.com/somedirectory/fileineed.txt"
            Dim myFileName = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "fileineed.txt")
            My.Computer.Network.DownloadFile(myURL, myFileName)

    If the file name is dynamic, then we would need to know a little bit more about the scenario, and then could possibly provide more help.

    Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com

  • Wednesday, September 19, 2012 7:41 PM
     
     
    Thanks-I beleive only excutable files or other types .zip etc get that question. The files are not yet up on the web site, but any .txt file on any web site usually just opens-I think?
  • Wednesday, September 19, 2012 7:46 PM
     
     

    Thanks- This is close, but there will be about 5-10 .txt files that will be on the site which will get added to from time to time, so I can't put a specific name in the code. i was able to use Iexplore.exe to turn the text files into install files. When I use that I get asked where to store it, which works fine, but a little cumbersome. I'd rather have my application's user just click on the web site file and it automatically opens the file for use in my program.

    Thanks Again

  • Wednesday, September 19, 2012 9:21 PM
     
     
    Thanks-I beleive only excutable files or other types .zip etc get that question. The files are not yet up on the web site, but any .txt file on any web site usually just opens-I think?

    That depends on the MIME settings of the webhost (and they're not all the same).

    Please call me Frank :)

  • Wednesday, September 19, 2012 9:23 PM
     
     

    Thanks- This is close, but there will be about 5-10 .txt files that will be on the site which will get added to from time to time, so I can't put a specific name in the code. i was able to use Iexplore.exe to turn the text files into install files. When I use that I get asked where to store it, which works fine, but a little cumbersome. I'd rather have my application's user just click on the web site file and it automatically opens the file for use in my program.

    Thanks Again

    Jerry,

    Consider doing it another way. Have a look at the code that I put together on a page of my website here. I'm reading a text file which is also on my website and displaying the result in a RichTextBox. I'm not suggesting that you show the RTB - I'm showing that you have the string in a variable and from there you can do anything you want with it:


    Please call me Frank :)

  • Wednesday, September 19, 2012 10:13 PM
     
     
    If you click on this link www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt a .txt file will appear in your webbrowsers window. But if you right click on it you can save it to file anyhow so I'm not sure how you would implement a procedure where you would click on it and auto save it to a file name

    You've taught me everything I know but not everything you know.


  • Wednesday, September 19, 2012 10:20 PM
     
     
    If you click on this link <cite>www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt a .txt file will appear in your webbrowsers window. But if you right click on it you can save it to file anyhow so I'm not sure how you would implement a procedure where you would click on it and auto save it to a file name.</cite>

    You've taught me everything I know but not everything you know.

    Then you didn't look at what I put together.

    Using the method that I showed, you have the entire string stored in a variable; from there just write it out to file.

    Try the code that I showed and use the URL that you show there. Don't even include an RichTextBox but instead, use the following:

    If testMe IsNot Nothing Then
         Stop
    End If

    When the program halts, hover your mouse over the variable "testMe" and you'll see the text from that site.


    Please call me Frank :)

  • Wednesday, September 19, 2012 10:41 PM
     
     
  • Wednesday, September 19, 2012 10:44 PM
     
     

    I suppose if the only function of the webbrowser is, when the application launches it, direct navigation to the websites page of .txt file listings occurs. So the users only choice when using the webbrowser is to click on a link to a .txt file. But what if the user were to click on a link to something other than a .txt file?

    In other words how to determine that only a link to a .txt file will be open for reading into the string and not for example a link that opens another web page so the web page gets read into the string?

    As an example what if the user went here http://www.cl.cam.ac.uk/~mgk25/ucs/examples/ and clicked on Name, Last Modified, Size, Description or User directory instead of a .txt file? I don't see any way to test for an actual .txt file at the link being clicked on. And the application is going to do something with whatever is in the string.

    The OP says "I'd rather have my application's user just click on the web site file and it automatically opens the file for use in my program." And application users don't necessarily follow the path of least resistance.

    Maybe I'm being too overly cautious about this or maybe at the OP's customers website webpage being navigated to there is nothing other than .txt files listed. It's probably not a concern really.


    You've taught me everything I know but not everything you know.

  • Wednesday, September 19, 2012 10:49 PM
     
     

    Try it with either overload of my function and see what it returns... that's why there's exception handling.


    Please call me Frank :)

  • Wednesday, September 19, 2012 11:00 PM
     
     

    I trust your code implicitly Frank. :)

    I just don't know what would happen with the OP's application if the user clicked on something other than one of the .txt files.


    You've taught me everything I know but not everything you know.

  • Wednesday, September 19, 2012 11:06 PM
     
     

    I trust your code implicitly Frank. :)

    I just don't know what would happen with the OP's application if the user clicked on something other than one of the .txt files.


    You've taught me everything I know but not everything you know.

    I assumed he'd be passing in the URL(s) directly ... if not then no, nothing I showed has any bearing on anything, but at that point, nothing will prevent the web browser from doing what it was programmed to do.

    Jerry, so we don't get any further confused here ... will you (or can you) pass the URL in directly?


    Please call me Frank :)

  • Thursday, September 20, 2012 1:18 AM
     
     
    Thanks guys- There's a lot of good information here and it sounds like exactly what I'm looking for, It will take me a few days to try it as I have to go on a trip until Friday. However, I will try it when I return and let you know.   The application does some testing of the file that it's the right length etc, so the user would get an error if something was picked in error.
  • Thursday, September 20, 2012 2:11 AM
     
     

    Frank- I was able to try the code quickly. It worked great. The only other thing is, I need the capability  for the user to be able to select from a bunch of txt files that will be listed on the web site and not known ahead of time?

    Thanks Again

  • Thursday, September 20, 2012 2:30 AM
     
     

    Frank- I was able to try the code quickly. It worked great. The only other thing is, I need the capability  for the user to be able to select from a bunch of txt files that will be listed on the web site and not known ahead of time?

    Thanks Again


    I also want to add that my application has to run on PC versions back to Windows XP.
  • Thursday, September 20, 2012 1:46 PM
     
     

    Frank- I was able to try the code quickly. It worked great. The only other thing is, I need the capability  for the user to be able to select from a bunch of txt files that will be listed on the web site and not known ahead of time?

    Thanks Again


    I guess that I'm confused. So the user will actually visit a web page in a browser? Is there no other way to retrieve the URL's of the links? It seems that that could be done but I don't know enough about your program to say definitively.

    Please call me Frank :)

  • Thursday, September 20, 2012 5:20 PM
     
     
    You think you're confused, I'm positively lost! :()

    You've taught me everything I know but not everything you know.

  • Thursday, September 20, 2012 10:52 PM
     
     
    You think you're confused, I'm positively lost! :()

    You've taught me everything I know but not everything you know.

    Ha!


    Please call me Frank :)

  • Friday, September 21, 2012 12:26 PM
     
     

    Sorry for the confusion. I'll try to explain my application requirement.--

    I have an application that needs to download new sets of calibration data via USB port as they become available to an instument that I designed for  company X. I have that part of the code working fine. The sets of calibration data will be put on the company X web site when they become available. For example, they would have links to a list of text data files on their website such as;

    Cal_Data_1.txt

    Cal_Data_2.txt

    Cal_Data_3.txt

    etc

    I would like to have the program have the capabilty to automaitically go to the web site using;

    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            WebBrowser1.Navigate(TextBox1.Text)
        End Sub

        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        End Sub
    End Class

    That works fine also. Now when one navigates to one of the files, example(Cal_Data_2), I simply need to put that file name into a string, then I could use that string in the code you sent me above...

    GetTextFromURL("string_URL_/Cal_data_2.txt") and it will put the data into the string you called "testMe".

    My customer would then distribute this application to their customers Y along with the instrument to keep it updated. The application that customer Y gets needs to run on PC's with Windows XP or later.

    I hope I didn't confuse you further.

    Thanks


    jerry


    • Edited by jerryls Friday, September 21, 2012 12:28 PM
    •  
  • Friday, September 21, 2012 1:15 PM
     
     

    Jerry,

    I'm getting a bit outside my knowledge base here, but you don't need a WebBrowser (assuming what you stated above is all that it's used for).

    What you need is the URL that the WebBrowser would go to. Will that be known in advance or how does the user know where to navigate to (ergo, how do they know what to type into the TextBox)?

    I'll explain more when you reply.


    Please call me Frank :)

  • Friday, September 21, 2012 1:59 PM
     
     

    Frank,

    No i don't need the web browser. I could fix the know URL to go to the page that has the list of text files. Your example has the URL including the UTF-8-test.txt name in it. I wanted to just have the user click on the particular file on the page.


    jerry


    • Edited by jerryls Friday, September 21, 2012 2:00 PM
    •  
  • Friday, September 21, 2012 2:04 PM
     
     

    Frank,

    No i don't need the web browser. I could fix the know URL to go to the page that has the list of text files. Your example has the URL including the UTF-8-test.txt name in it. I wanted to just have the user click on the particular file on the page.


    jerry


    I don't think you're following what I mean.

    You can just as easily download the .htm page that the user would type using the same method and save it as a text file.

    If so, then you now have a means to parse through that .html page and locate the links that would be displayed in the browser as a link to the text file.

    Make sense?

    Try the second version (overload) of the function and use the URL that the user would type in and have it save it to your desktop as a text file. Open it with NotePad and see if you can manually spot those links.

    If you can then you can write a method to do it automatically - then you have the URL's to the various text files that would show up as links in the browser.


    Please call me Frank :)

  • Friday, September 21, 2012 2:31 PM
     
     

    Jerry,

    I've added another overload that I'll get to momentarily. First have a look at a sample HTML page with links to text files that I created here:

    http://www.fls-online.com/VBNet_Forum/09-21-12/Sample_Links.htm

    I modified the function to now include a method that will read the text line-by-line rather than all at once and I show an example of how to use in the form's load event. That's shown here:

    http://www.fls-online.com/VBNet_Forum/09-21-12/ReadTextFileOnline.htm

    The text file (which is the .htm file) that it returned is shown below:

    * * *

    ... never mind on that one - this is seeing it as HTML and displaying as the page again!

    Try it yourself though and open the text file up. You can see the links in it.

    Does that give you an idea of how it might could be done?


    Please call me Frank :)

  • Saturday, September 22, 2012 3:30 PM
     
     

    Thanks Frank-

    This method does work, but the file is loaded with html text and I would have to search for the ".txt" files or something to choose them. Also they wouldn't be in the exact same place every time, if any changes were made to the web page. There's got to be a way to get the information(file name or URL address of file name) that the user browses to.- I would think.

    Thanks


    jerry


    • Edited by jerryls Saturday, September 22, 2012 3:30 PM
    •  
  • Saturday, September 22, 2012 3:32 PM
     
     

    Thanks Frank-

    This method does work, but the file is loaded with html text and I would have to search for the ".txt" files or something to choose them. Also they wouldn't be in the exact same place every time, if any changes were made to the web page. There's got to be a way to get the information(file name or URL address of file name) that the user browses to.- I would think.

    Thanks


    jerry


    There is a way to get the links (or specifically, the URL that the link is pointing to). That's the part where I said earlier that I'm really out of my element about, but I know it can be done - in fact I've seen it posted here several times over the years.

    That might be worth another question specifically asking how you can parse through HTML and look for the URL of all links which refer to a .txt file.


    Please call me Frank :)

  • Saturday, September 22, 2012 4:39 PM
     
     Answered

    Thanks Frank-

    This method does work, but the file is loaded with html text and I would have to search for the ".txt" files or something to choose them. Also they wouldn't be in the exact same place every time, if any changes were made to the web page. There's got to be a way to get the information(file name or URL address of file name) that the user browses to.- I would think.

    Thanks


    jerry


    There is a way to get the links (or specifically, the URL that the link is pointing to). That's the part where I said earlier that I'm really out of my element about, but I know it can be done - in fact I've seen it posted here several times over the years.

    That might be worth another question specifically asking how you can parse through HTML and look for the URL of all links which refer to a .txt file.


    Please call me Frank :)


    Thanks for all your help Frank- I really appreciate it. I'm thinking the webBrowser1.navigation function is so simple to use must be able to get at the file. If it puts it on the screen, then there must be a way to simply store it into a text string.

    jerry


    • Marked As Answer by jerryls Saturday, September 22, 2012 5:03 PM
    • Edited by jerryls Saturday, September 22, 2012 7:07 PM
    •  
  • Saturday, September 22, 2012 6:35 PM
     
     

    Jerry,

    Would you try something?

    It seems to work but it's only been tested using one site which I have here. That's a revised version of what I posted yesterday and includes one link to a text file which isn't directly in that same directory.

    The revised code is shown here and there's a new function which - in theory - will return a list of all links on a web page. I then filter that list down to get only those which point to a text file.

    Again ... in theory!

    Try it on your real stuff and see if it works?


    Please call me Frank :)