Craig Shearer
Craig Shearer

Reputation: 14521

In asp.net mvc2 how to open a new window and download a file

In my ASP.NET MVC2 app, I want to have a button in my view that collects some parameters off some form fields, then calls a controller action that returns a FileResult to stream a file (according to the parameters) to a new brower window.

I'm fine with using jquery AJAX to call the controller action, passing the paramters, but how do I get a new window to open for the streamed file?

EDIT: I know how to do stream the file back to the client browser - I'm just asking about how to write the View to collect some parameters to pass to the action.

For example, I have a fairly complex form, divided up into tabs using jquery.tabs(). On one of the tabs, the user can enter a dollar amount into a textbox, choose a template from the drop-down list then click a button to generate a voucher. So, I need to collect the values off the dollar amount box and drop-down selection (and I want to validate these, client-side) then call the controller action to generate a file to stream back to the browser.

So, my queestion is mainly a noob MVC question - what do I write in the view to specify a "mini form" that contains just those two fields, then validate them (I think I'm OK with that) then pass them to a controller action? (And I'm fine with using the link and specifying the target as _blank to open a new window.)

Upvotes: 2

Views: 1385

Answers (2)

JAEP
JAEP

Reputation: 383

If I understand well, you must create a MemoryStream object

MemoryStream m = new MemoryStream();

Fill the object and then

HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/ms-word";
HttpContext.Current.Response.AddHeader("Content-Disposition","attachment;filename=document.doc");
HttpContext.Current.Response.BinaryWrite(m.GetBuffer());
HttpContext.Current.Response.End();

Here the list of the mime-type allowed

http://msdn.microsoft.com/en-us/library/ms775147%28VS.85%29.aspx

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

You usually don't call controller actions returning files using AJAX because you won't be able to do much with this file in the success callback (you cannot store the file to the client computer). So there are two possibilities: either the user is prompted with a dialog box to choose the location he wants to save the file or the contents of the file is opened inline with the default associated program (if there is any). It is the Content-Disposition header which is used to control this behavior:

Control-Disposition: attachment;filename=foo.pdf

will show a Save As dialog box to the user and it will stay on the same page. There won't be any redirect happening.

Control-Disposition: inline;filename=foo.pdf

will try to open the contents of the file inside the browser if there is an associated program and it will replace the current page.

If you want to open the contents of the file in a new browser window you could use target="_blank":

<a href="/someaction" target="_blank">Download</a>

Obviously this is only necessary if you are opening the file inline. For files that should be saved to the user computer you should simply provide the link and leave the user decide what to do with the file. No AJAX.

Upvotes: 3

Related Questions