Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC how to make an action that return a file?

Hello I am generating an excel file inside my action that I would return to the user. How do I have to declare the action to return a file?

May I call this action with ajax?

thanks!

like image 271
Lorenzo Avatar asked Oct 14 '10 16:10

Lorenzo


People also ask

What is the return type of action result in MVC?

An ActionResult is a return type of a controller method in MVC. Action methods help us to return models to views, file streams, and also redirect to another controller's Action method.

What is return type of action?

Action Result is actually a data type. When it is used with action method, it is called return type. As you know, an action is referred to as a method of the controller, the Action Result is the result of action when it executes. In fact, Action Result is a return type.

Can I return ActionResult Instead of view?

First to Understand---ActionResult and ViewResult are basically a return type for an method(i.e Action in MVC). Second The Difference is --- ActionResult can return any type of Result Whereas ViewResult can return result type of View Only.


1 Answers

Option 1: Use built in ActionResults, such as FilePathResult, FileStreamResult, FileContentResult

Option 2: Build a custom ActionResult to return your excel file (example: http://www.dotnetcurry.com/ShowArticle.aspx?ID=484&AspxAutoDetectCookieSupport=1)

like image 67
Retired_User Avatar answered Sep 29 '22 09:09

Retired_User