0

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.

Ademar
  • 1,418
  • 1
  • 17
  • 27
  • What does that action (`"ExcelExport"`) return? If it returns a `File()` then there should indeed be a Save File dialog, unless of course the browser is configured not to display it. You can't really display the dialog directly in a web application. What you can do is send a file to the client, and let the client decide what to do with it. – David Sep 26 '13 at 10:29
  • _"I want to display Savefile dialog after click to that"_. That is not up to you. The browser decides what to do with a file download. See [How to show 'Save as' dialog box using PHP for text files](http://stackoverflow.com/questions/732063/how-to-show-save-as-dialog-box-using-php-for-text-files). – CodeCaster Sep 26 '13 at 10:30

1 Answers1

3

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.

David
  • 208,112
  • 36
  • 198
  • 279