File download with mvc is giving a stick

1

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

As it comes to the web, this can only be done via download, and all examples searched (at least for me), I fall into FileResult . Turns out it's not working.

I took a simple example and it 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);
}

In this line I get the error

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

This is the error:

  

Non-invocable member 'File' can not be used like method.

How do I resolve this error?

    
asked by anonymous 18.09.2018 / 17:06

1 answer

2

The problem is that you are almost certainly using the System.IO.File class, which is static, so it can not 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);

    
18.09.2018 / 17:32