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
  1. Home
  2. General Programming
  3. C#
  4. Download file from server to local folder

Download file from server to local folder

Scheduled Pinned Locked Moved C#
helpsysadminworkspace
16 Posts 6 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 Agweet

    Hi All, i am trying to download a file from my website on my server, but i keep getting a error: Access to the path 'c:\ImageName.gif' is denied. this is my code i am using: using (WebClient wcDownload = new WebClient()) { try { webRequest = (HttpWebRequest)WebRequest.Create("http://www.XXXXXXXXXXX.XX.XX/Images/" + strImgName); webRequest.Credentials = CredentialCache.DefaultCredentials; webResponse = (HttpWebResponse)webRequest.GetResponse(); Int64 fileSize = webResponse.ContentLength; strResponse = wcDownload.OpenRead("http://www.XXXXXXXXXXX.XX.XX/Images/" + strImgName); string strFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); strLocal = new FileStream(strFolder + "\\" + strImgName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); int bytesSize = 0; byte[] downBuffer = new byte[2048]; while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0) { strLocal.Write(downBuffer, 0, bytesSize); } string strAlert = "alert('" + strImgName + " downloaded to " + strFolder + "');"; ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", strAlert, true); } finally { strResponse.Close(); strLocal.Close(); } } Any help is much appreciated. living life on the flip side

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #2

    Windows, since Vista, is rather picky on the locations it lets you create files. Have you tried a different path at all? :)

    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

    Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

    1 Reply Last reply
    0
    • A Agweet

      Hi All, i am trying to download a file from my website on my server, but i keep getting a error: Access to the path 'c:\ImageName.gif' is denied. this is my code i am using: using (WebClient wcDownload = new WebClient()) { try { webRequest = (HttpWebRequest)WebRequest.Create("http://www.XXXXXXXXXXX.XX.XX/Images/" + strImgName); webRequest.Credentials = CredentialCache.DefaultCredentials; webResponse = (HttpWebResponse)webRequest.GetResponse(); Int64 fileSize = webResponse.ContentLength; strResponse = wcDownload.OpenRead("http://www.XXXXXXXXXXX.XX.XX/Images/" + strImgName); string strFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); strLocal = new FileStream(strFolder + "\\" + strImgName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); int bytesSize = 0; byte[] downBuffer = new byte[2048]; while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0) { strLocal.Write(downBuffer, 0, bytesSize); } string strAlert = "alert('" + strImgName + " downloaded to " + strFolder + "');"; ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", strAlert, true); } finally { strResponse.Close(); strLocal.Close(); } } Any help is much appreciated. living life on the flip side

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #3

      Are you trying to run this from your website? If so, you will probably find that Environment.SpecialFolder.DesktopDirectory doesn't return a value that you'd expect because the IIS user doesn't have a desktop.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      1 Reply Last reply
      0
      • A Agweet

        Hi All, i am trying to download a file from my website on my server, but i keep getting a error: Access to the path 'c:\ImageName.gif' is denied. this is my code i am using: using (WebClient wcDownload = new WebClient()) { try { webRequest = (HttpWebRequest)WebRequest.Create("http://www.XXXXXXXXXXX.XX.XX/Images/" + strImgName); webRequest.Credentials = CredentialCache.DefaultCredentials; webResponse = (HttpWebResponse)webRequest.GetResponse(); Int64 fileSize = webResponse.ContentLength; strResponse = wcDownload.OpenRead("http://www.XXXXXXXXXXX.XX.XX/Images/" + strImgName); string strFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); strLocal = new FileStream(strFolder + "\\" + strImgName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); int bytesSize = 0; byte[] downBuffer = new byte[2048]; while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0) { strLocal.Write(downBuffer, 0, bytesSize); } string strAlert = "alert('" + strImgName + " downloaded to " + strFolder + "');"; ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", strAlert, true); } finally { strResponse.Close(); strLocal.Close(); } } Any help is much appreciated. living life on the flip side

        A Offline
        A Offline
        Agweet
        wrote on last edited by
        #4

        Hi All, thanks for the replies, i haven't tried a different path yet. is there anyway i can get this download to work. all i really need to do is allow the user to download the image from the website which is hosted on the server to their local machine, preferably their desktop. living life on the flip side

        R 1 Reply Last reply
        0
        • A Agweet

          Hi All, thanks for the replies, i haven't tried a different path yet. is there anyway i can get this download to work. all i really need to do is allow the user to download the image from the website which is hosted on the server to their local machine, preferably their desktop. living life on the flip side

          R Offline
          R Offline
          Ravi Bhavnani
          wrote on last edited by
          #5

          Agweet wrote:

          i haven't tried a different path yet. is there anyway i can get this download to work.

          Isn't that a bit like going to the doctor and asking him to cure you but refusing to follow his advice? :confused: /ravi

          My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

          L 1 Reply Last reply
          0
          • R Ravi Bhavnani

            Agweet wrote:

            i haven't tried a different path yet. is there anyway i can get this download to work.

            Isn't that a bit like going to the doctor and asking him to cure you but refusing to follow his advice? :confused: /ravi

            My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #6

            it may be OK to ask for advice, then not follow it. However asking again is a bit weird, yes. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            OriginalGriffO 1 Reply Last reply
            0
            • L Luc Pattyn

              it may be OK to ask for advice, then not follow it. However asking again is a bit weird, yes. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #7

              Luc Pattyn wrote:

              it may be OK to ask for advice, then not follow it. However asking again is a bit weird, yes.

              You've clearly not handled technical support calls from idiots non-technical users!

              Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              A L 2 Replies Last reply
              0
              • OriginalGriffO OriginalGriff

                Luc Pattyn wrote:

                it may be OK to ask for advice, then not follow it. However asking again is a bit weird, yes.

                You've clearly not handled technical support calls from idiots non-technical users!

                Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

                A Offline
                A Offline
                Agweet
                wrote on last edited by
                #8

                well, 1. i am not an idiot, and that was not truly called for, but anyways, 2. i did however try a new path after i posted reply, no change, still get the error. is it a permissions issue with the server accessing the local file? or is there something else i am missing? living life on the flip side

                R OriginalGriffO 3 Replies Last reply
                0
                • OriginalGriffO OriginalGriff

                  Luc Pattyn wrote:

                  it may be OK to ask for advice, then not follow it. However asking again is a bit weird, yes.

                  You've clearly not handled technical support calls from idiots non-technical users!

                  Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #9

                  OriginalGriff wrote:

                  You've clearly not handled technical support calls from non-technical users!

                  I seem to find ways that ensure they don't come back... :laugh:

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                  OriginalGriffO 1 Reply Last reply
                  0
                  • A Agweet

                    well, 1. i am not an idiot, and that was not truly called for, but anyways, 2. i did however try a new path after i posted reply, no change, still get the error. is it a permissions issue with the server accessing the local file? or is there something else i am missing? living life on the flip side

                    R Offline
                    R Offline
                    Ravi Bhavnani
                    wrote on last edited by
                    #10

                    Are you able to manually (i.e. thru the Windows UI) create a file with the same path name? That would help identify if it's a permissions issue. /ravi

                    My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                    1 Reply Last reply
                    0
                    • A Agweet

                      well, 1. i am not an idiot, and that was not truly called for, but anyways, 2. i did however try a new path after i posted reply, no change, still get the error. is it a permissions issue with the server accessing the local file? or is there something else i am missing? living life on the flip side

                      R Offline
                      R Offline
                      Ravi Bhavnani
                      wrote on last edited by
                      #11

                      Just for grins, does this work any better?

                      WebClient wc = new WebClient();
                      wc.DownloadFile ("http://.../foo.jpg", @"C:\MyFolder\foo.jpg");

                      You may want to save the image to your user's application data path (to which you have write access). /ravi

                      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                      A 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        OriginalGriff wrote:

                        You've clearly not handled technical support calls from non-technical users!

                        I seem to find ways that ensure they don't come back... :laugh:

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #12

                        Please oh great one! Tell me your secrets! :laugh: [edit]"oh", you clumsy typist Griff, "oh" - not "of"[/edit]

                        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        1 Reply Last reply
                        0
                        • A Agweet

                          well, 1. i am not an idiot, and that was not truly called for, but anyways, 2. i did however try a new path after i posted reply, no change, still get the error. is it a permissions issue with the server accessing the local file? or is there something else i am missing? living life on the flip side

                          OriginalGriffO Offline
                          OriginalGriffO Offline
                          OriginalGriff
                          wrote on last edited by
                          #13

                          Agweet wrote:

                          i am not an idiot, and that was not truly called for

                          I'm sorry - did I call you an idiot? If you look at the threads, I was talking to Luc, and making a generic comment about technical support phone calls. Trust me, if I wanted to insult you, you would know all about it! :laugh:

                          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                          1 Reply Last reply
                          0
                          • R Ravi Bhavnani

                            Just for grins, does this work any better?

                            WebClient wc = new WebClient();
                            wc.DownloadFile ("http://.../foo.jpg", @"C:\MyFolder\foo.jpg");

                            You may want to save the image to your user's application data path (to which you have write access). /ravi

                            My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                            A Offline
                            A Offline
                            Agweet
                            wrote on last edited by
                            #14

                            Hi Ravi, thanks for the reply, i tried downloading the file to the users application data but still get the error.

                            living life on the flip side

                            R 1 Reply Last reply
                            0
                            • A Agweet

                              Hi All, i am trying to download a file from my website on my server, but i keep getting a error: Access to the path 'c:\ImageName.gif' is denied. this is my code i am using: using (WebClient wcDownload = new WebClient()) { try { webRequest = (HttpWebRequest)WebRequest.Create("http://www.XXXXXXXXXXX.XX.XX/Images/" + strImgName); webRequest.Credentials = CredentialCache.DefaultCredentials; webResponse = (HttpWebResponse)webRequest.GetResponse(); Int64 fileSize = webResponse.ContentLength; strResponse = wcDownload.OpenRead("http://www.XXXXXXXXXXX.XX.XX/Images/" + strImgName); string strFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); strLocal = new FileStream(strFolder + "\\" + strImgName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); int bytesSize = 0; byte[] downBuffer = new byte[2048]; while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0) { strLocal.Write(downBuffer, 0, bytesSize); } string strAlert = "alert('" + strImgName + " downloaded to " + strFolder + "');"; ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", strAlert, true); } finally { strResponse.Close(); strLocal.Close(); } } Any help is much appreciated. living life on the flip side

                              J Offline
                              J Offline
                              johannesnestler
                              wrote on last edited by
                              #15

                              Sorry, I have no solution for you. Just a question: Why don't you use WebClient.DownloadFile for downloading the image? Anyway, your code seems to be correct... P.S. Have you heard about Path.Combine (because

                              +"\\"

                              doesn't look so good)

                              1 Reply Last reply
                              0
                              • A Agweet

                                Hi Ravi, thanks for the reply, i tried downloading the file to the users application data but still get the error.

                                living life on the flip side

                                R Offline
                                R Offline
                                Ravi Bhavnani
                                wrote on last edited by
                                #16

                                Is is possible that the target file has already been opened (for exclusive read or write), thereby preventing it from being overwritten?  Although I think if that were the case you'd see a different exception. /ravi

                                My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                                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