简体   繁体   中英

Downloading file from my project

I have a controller with a method for downloading file. Without code, all I need is to go to this link:

http://localhost:1186/Content/MyFolder/file1.exe

and the file gets downloaded.

I tried to do this with code like this:

Response.Redirect(Server.MapPath("~\\Content\\MyFolder\\file1.exe"));

But the breakpoint passes through this line and nothing happens. I think the problem is I'm using Server.MapPath, but how else would I do this?

you can do something like this

create an action

public ActionResult RedirectToDownload()
{
    return View();
}

and then on the view with JavaScript on load redirect to your URL

$(document).ready(function() {
  window.navigate("~/Content/MyFolder/file1.exe");
});

you might also use this

public ActionResult Index()
{
    return Redirect("~/Content/MyFolder/file1.exe");
}

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