0

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?

7
  • Are you trying to trigger a download prompt, or just return a file for your JavaScript code to interpret in some fashion? Commented Feb 5, 2020 at 22:21
  • I just want to open the typical download window where client can save the file to his PC Commented Feb 5, 2020 at 22:22
  • the fileName also need to have the extension Commented Feb 5, 2020 at 22:22
  • 1
    This is an HTTP endpoint, so call it like you would any other HTTP endpoint. Commented Feb 5, 2020 at 22:22
  • could any of you provide some code example please? Commented Feb 5, 2020 at 22:24

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.