简体   繁体   中英

download a file from server using a controller and javascript

I have the controller code below:

public FileResult DownloadFileParaView()
{
    byte[] fileBytes = System.IO.File.ReadAllBytes(@"MyPath");
    string fileName = "MyFileName";
    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}

How can I call it from JavaScript to return my file? Does I have to copy the file to project content?

Assuming you're calling this from within a view, it's relatively simple:

window.location.href = '@Url.Action("DownloadFileParaView")';

This would be in a script tag within the view where you want to trigger the download.

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