在asp.net mvc2中如何打开新窗口并下载文件
在我的 ASP.NET MVC2 应用程序中,我希望在视图中有一个按钮,该按钮从某些表单字段收集一些参数,然后调用返回 FileResult 的控制器操作,以将文件(根据参数)流式传输到新的浏览器窗口。
我可以使用 jquery AJAX 调用控制器操作,传递参数,但是如何获取为流文件打开的新窗口?
编辑:我知道如何将文件流式传输回客户端浏览器 - 我只是询问如何编写视图来收集一些参数以传递给操作。
例如,我有一个相当复杂的表单,使用 jquery.tabs() 分为多个选项卡。在其中一个选项卡上,用户可以在文本框中输入美元金额,从下拉列表中选择模板,然后单击按钮生成凭证。因此,我需要收集美元金额框和下拉选择的值(并且我想在客户端验证这些值),然后调用控制器操作来生成一个文件以流回浏览器。
所以,我的问题主要是一个菜鸟 MVC 问题 - 我在视图中写什么来指定只包含这两个字段的“迷你表单”,然后验证它们(我认为我对此没问题),然后将它们传递给控制器动作? (我可以使用链接并将目标指定为 _blank 以打开新窗口。)
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您通常不会使用 AJAX 调用返回文件的控制器操作,因为您无法在成功回调中对此文件执行太多操作(您无法将该文件存储到客户端计算机)。因此有两种可能性:要么通过对话框提示用户选择要保存文件的位置,要么使用默认关联程序(如果有)内联打开文件内容。
Content-Disposition
标头用于控制此行为:将向用户显示“另存为”对话框,并且它将保留在同一页面上。不会发生任何重定向。
如果有关联的程序,将尝试在浏览器中打开文件的内容,并将替换当前页面。
如果您想在新的浏览器窗口中打开文件的内容,您可以使用
target="_blank"
:显然,仅当您
内联
打开文件时才需要这样做。对于应保存到用户计算机的文件,您只需提供链接并让用户决定如何处理该文件。没有阿贾克斯。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: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.
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"
: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.如果我理解得很好,您必须创建一个 MemoryStream 对象
填充该对象,然后
这里是允许的 mime 类型列表
http://msdn.microsoft.com/en-us/library/ms775147%28VS.85%29.aspx
If I understand well, you must create a MemoryStream object
Fill the object and then
Here the list of the mime-type allowed
http://msdn.microsoft.com/en-us/library/ms775147%28VS.85%29.aspx