Download file with mvc is giving dick

Asked

Viewed 61 times

1

I did several searches on the net to find a way to generate a spreadsheet and save to disk.

As it comes to web, this can only be done via download, and all the examples searched (at least by me), I fall in the Fileresult. Turns out it’s not working.

I took a simple example and gives the same error. See the code

public FileResult Download()
{
    byte[] fileBytes = System.IO.File.ReadAllBytes(@"c:\folder\myfile.ext");
    string fileName = "myfile.ext";
    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}

On that line I get the error

return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);

That is the mistake:

Non-invocable Member 'File' cannot be used like a method.

How do I fix this mistake?

1 answer

2


The problem is that you are almost certainly using the class System.IO.File, which is static, so cannot be instantiated or returned.

Try changing the line

return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);

To:

return System.Web.Mvc.File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);

  • Says that File does not exists in the Namespace System.Web.Mvc and if change to Fileresult gives the error of the original post

  • Strange, according to the Microsoft documentation this exists: https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.file(v=vs.118). aspx#M:System.Web.Mvc.Controller.File%28System.String,System.String,System.String%29

  • Reading the documentation now I understood that I could not use outside of a controller. Already fixed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.