0

I am working on asp.net (mvc) project I have to use method which provides possibility to download files by its url and download it into project map ~/App_Data/uploads/Docs to this path, and I cant find method to specify where the file will be saved.

The method which I found to download by URL:

using (var client = new WebClient())
{
    string pathAndName = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/uploads/Docs"), "stack.png");
    client.DownloadFile("https://www.gravatar.com/avatar/51d065150eba7bfe5b6379e4caaaae88?s=328&d=identicon&r=PG", pathAndName);
}

How could I fix that issue?

GeekyNuns
  • 287
  • 2
  • 20
  • See also [Writing data to App_Data](http://stackoverflow.com/questions/2574587/writing-data-to-app-data). – CodeCaster Sep 23 '16 at 14:07
  • dang! I was just writing an answer to this – Simon Price Sep 23 '16 at 14:10
  • the end of your line of code `"stack.png");` is where the problem is occurring as you need to give it a path. for instance is `"stack.png");` was changed to "c:\\temp\\stack.png" that would then download your file to c:\temp. this is just psudo code, but should give you an indication of where you want to save it if you want to use special folders `Path.Combine(Environment.SpecialFolder.MyDocuments.ToString(), "downloadFolder", "stack.png");` which will return as > MyDocuments\downloadFolder\stack.png – Simon Price Sep 23 '16 at 14:10
  • @Simon well, I can advise you to first search for a duplicate before starting to write an answer, as a _lot_ of questions that get asked per day have in fact been asked and answered before. As for your comment: you don't want a web server to download to My Documents, and the OP mentioned the AppData folder. – CodeCaster Sep 23 '16 at 14:14
  • I have tried this as second parametr of DownloadFile function `"C:\\Users\\DmitrijsS\\Desktop"+newDoc.docFileName` but the file was not saved – GeekyNuns Sep 23 '16 at 14:14
  • 1
    @Geeky yes it was, but not on the desktop, as you're missing a backslash. It will be saved to `"C:\\Users\\DmitrijsS\\DesktopSomeFilename"`. Again, see duplicate and the question from my first comment to get your answer. Also, use `Path.Combine()` to form paths, not string concatenation. – CodeCaster Sep 23 '16 at 14:15
  • @CodeCaster that folder was just an example – Simon Price Sep 23 '16 at 14:16
  • heres the edited path ` Path.Combine(Environment.SpecialFolder.ApplicationData.ToString(), "downloadFolder", "stack.png");` – Simon Price Sep 23 '16 at 14:16
  • I edited, this is the way it should work? I am sorry for being so slow today... but I really cant get it. in this case Server keyword `does not exist` – GeekyNuns Sep 23 '16 at 14:27
  • Search before asking (or editing a new question into your question). See [Using Server.MapPath in MVC3](http://stackoverflow.com/questions/7600118/using-server-mappath-in-mvc3). – CodeCaster Sep 23 '16 at 14:29
  • Sorry, just have 20 minutes to deadline.. and hate to create report as well, so I wanted to get an answer while I am writing report, without searching on my own, I admit that it is bad thing to do... – GeekyNuns Sep 23 '16 at 14:32

0 Answers0