ASP.NET MVC: loading an excel file

I load an excel file into a C # action method, reutrns FileResult, for example:

return File(bindata, "application/octet-stream", "mytestfile.xls"); 

When I manually navigate to the URL corresponding to the above method, I get a visualized representation of the file. The file will not load using Save As -dialog.

Is there a way to make the download happen using Save As -dialog?

-pom -

+6
source share
3 answers

I have the feeling that you are getting this behavior due to the return type of media.

Try changing the media type to application / vnd.ms-excel as follows:

 return File(bindata, "application/vnd.ms-excel", "mytestfile.xls"); 
+5
source

Usually, when you specify a file name for the File method, it automatically adds a Content-Disposition header so that the Save-As dialog box always displays. So I'm a little surprised when you say that your code is not working. You can also try manually setting this header:

 Response.AppendHeader("Content-Disposition", "attachment; filename=mytestfile.xls"); 
+8
source

Can you try this:

 return new FileContentResult(bindata, "application/vnd.ms-excel") { FileDownloadName = "mytestfile.xls") }; 

Hope this helps.

+4
source

Source: https://habr.com/ru/post/897159/


All Articles