SaveFile dialog in MVC razor on link click

2019-09-09 02:13发布

问题:

I would like to ask, how can I open savefile dialog in mvc razor4? I wanted use normal savefile dialog but I have found out that it can be used only in Win forms. So I search in google but didn't find good solution. Only that there is

<input type="file" /> 

but I think it is problematic to overwrite his look.

I have something like this:

<a href="@Url.Action("ExcelExport","Home")"><img src="@Url.Content("~/images/excel_icon.png")" id="excel-export-img" /></a>

I want to display Savefile dialog after click to that. Know anyone about some materials about it or how to do it?

Thak you.

回答1:

Keep in mind that HTTP doesn't necessarily have a concept of "files." It has requests and responses. And each of those has headers and content. So what you want is to return the contents of a file with a header indicating that it is a "file" and should be treated as such. It's still up to the browser how to actually handle it, of course.

The way to do this in ASP.NET MVC is to return a File() from your controller action. Something like this:

return File("SomeFile.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

There are, of course, several overloads for the File() method, some of which accept a byte array or stream instead of the name of an actual server-side file.