简体   繁体   中英

C# webClient->DownloadFile download don't start

I'm an absolutely beginner in C#-Programming and have one stupid Problem. I only like to download files, which are in the temporatry directory with WebClient.DownloadFile. The code is (Action starts with a button-click to the one of two buttons on the page):

[HttpPost]
[MultiButton(MatchFormKey = "action", MatchFormValue = "ABC")]
public ActionResult ABC(TestFormModel model)
{
  string fileName = model.resultLink;
  try
     {
       string tempPath = System.IO.Path.GetTempPath();
       System.IO.Directory.SetCurrentDirectory(tempPath);
       fileName = System.IO.Path.Combine(tempPath, fileName);

       Uri uri = new Uri(fileName);

       WebClient webClient = new WebClient();
       webClient.DownloadFile(uri, "FFF");                    
     }
  catch (Exception ex)
     { ... }                  
  return View(); 
}

Now I expected (reading "Downloads the resource with the specified URI to a local file." in the documentation) that the download dialog from browser will propmt. But NOTHING happens. No exception, no download dialog. What is wrong with my code?

Thank you very much!

The "local file" in this case is a file on the server (Local to the web server) and not local to the end user.

The idea here is that once you've downloaded a resourced from one location to your local server, you can redirect the user to that downloaded file and, if their browser is set up to do so, a dialog will appear allowing them to save the file.

By the looks of it you're downloading the file from your local server to your local server.

If the file already exists on your local server you can simply redirect the user at the end of your action method and their browser will handle the rest.

return Redirect(fileUrl);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM