Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Howto download a file from http-site

Howto download a file from http-site

Scheduled Pinned Locked Moved C#
tutorialcsharpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ariadne
    wrote on last edited by
    #1

    Hi, is there an example, how to download a (msi) file from a web-site (via http) to the local harddisk in C#? Thanks! Ariadne

    C 1 Reply Last reply
    0
    • A Ariadne

      Hi, is there an example, how to download a (msi) file from a web-site (via http) to the local harddisk in C#? Thanks! Ariadne

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Use HttpWebRequest[^] to issue the request for the MSI file, this will be returned in a HttpWebResponse[^] object. You can then stream the data to the local disk.


      My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

      A 1 Reply Last reply
      0
      • C Colin Angus Mackay

        Use HttpWebRequest[^] to issue the request for the MSI file, this will be returned in a HttpWebResponse[^] object. You can then stream the data to the local disk.


        My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

        A Offline
        A Offline
        Ariadne
        wrote on last edited by
        #3

        Thanks Colin, I did this: myReq=HttpWebRequest)WebRequest.Create("http://www.xyz.ch/file.msi"); myResponse = myReq.GetResponse(); ReceiveStream = myResponse.GetResponseStream(); FileStream frStream = new FileStream(ReceiveStream, FileAccess.Read); FileStream fwStream = new FileStream(Application.StartupPath+"file.msi",FileMode.Create,FileAccess.Write); But this does not work: ReceiveStream is not System.IO.Stream Ariadne

        C 1 Reply Last reply
        0
        • A Ariadne

          Thanks Colin, I did this: myReq=HttpWebRequest)WebRequest.Create("http://www.xyz.ch/file.msi"); myResponse = myReq.GetResponse(); ReceiveStream = myResponse.GetResponseStream(); FileStream frStream = new FileStream(ReceiveStream, FileAccess.Read); FileStream fwStream = new FileStream(Application.StartupPath+"file.msi",FileMode.Create,FileAccess.Write); But this does not work: ReceiveStream is not System.IO.Stream Ariadne

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          If I recall correctly, you need to pass the ResponseStream to a StreamReader. You can then read the contents of the stream to a byte array and then write the array that to your output file stream. The response stream will tell you how large you need to make the byte array (of course, you can always make it smaller and retrieve the information in chunchs by using multiple reads)


          My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

          A 1 Reply Last reply
          0
          • C Colin Angus Mackay

            If I recall correctly, you need to pass the ResponseStream to a StreamReader. You can then read the contents of the stream to a byte array and then write the array that to your output file stream. The response stream will tell you how large you need to make the byte array (of course, you can always make it smaller and retrieve the information in chunchs by using multiple reads)


            My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

            A Offline
            A Offline
            Ariadne
            wrote on last edited by
            #5

            Yes Colin, the canonical way works! thanks.:-O Ariadne

            J 1 Reply Last reply
            0
            • A Ariadne

              Yes Colin, the canonical way works! thanks.:-O Ariadne

              J Offline
              J Offline
              jkersch
              wrote on last edited by
              #6

              have you tried the WebClient class? WebClient w = new WebClient(); w.DownloadFile(address_str,file_str); //if you want to hold the file only in memory, try // w.DownloadData() //for parameters check msdn this is maybe a lot simpler than working with HttpWebRequest objects ;) best regards j.

              A 1 Reply Last reply
              0
              • J jkersch

                have you tried the WebClient class? WebClient w = new WebClient(); w.DownloadFile(address_str,file_str); //if you want to hold the file only in memory, try // w.DownloadData() //for parameters check msdn this is maybe a lot simpler than working with HttpWebRequest objects ;) best regards j.

                A Offline
                A Offline
                Ariadne
                wrote on last edited by
                #7

                Yes much more easy! thanks. I now use this WebClient version:) Ariadne

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups