Asp.net MVC FileContentResult - prevent开口浏览器 [英] Asp.net MVC FileContentResult - prevent opening in browser

查看:328
本文介绍了Asp.net MVC FileContentResult - prevent开口浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个控制器动作返回一个文件给用户。我想用户可以psented与下载(打开/保存)对话框中,无论文件类型$ P ​​$。这工作得很好,当文件类型为doc和.DOCX,的.xlsx等。,但是,当该文件是txt文件,.xps,.PDF(有时),或.html,它在浏览器中打开。

One of my controller actions returns a file to the user. I would like the user to be presented with the download (open/save) dialog, regardless of the file type. This works fine when the file type is .doc, .docx, .xlsx, etc.., but when the file is .txt, .xps, .pdf (sometimes), or .html, it opens in the browser.

有没有一种方法可以让我prevent从开盘在浏览器中的文件,只允许用户在一个单独的窗口中打开它,而不从当前页面导航了吗?

Is there a way I can prevent the file from opening in the browser, and only allow the user to open it in a separate window without navigating away from the current page?

该文件的请求使用由jQuery的 $。阿贾克斯({})

The request for the file is made using jQuery's $.ajax({}).

相关报道:具有浏览器处理该请求,并给予相对于AJAX调用接收文件作为响应字符串conent弹出由<一个解释href="http://stackoverflow.com/questions/2240869/asp-net-mvc-controller-filecontent-actionresult-called-via-ajax/2247061#2247061">this ansewr 的,但这个问题地址强制浏览器来处理文件以某种方式一旦被接收。

Related: Having the browser handle the request and give the popup as opposed to the AJAX call receiving the conent of the file as a response string is explained by this ansewr, but this question addresses forcing the browser to handle the file in a certain way once it is received.

推荐答案

我不认为你可以使用一个客户端命令,或者设置做到这一点。您需要做一些事情的服务器上,以便它返回应用程序/八位字节流的每个文件的内容类型。否则,浏览器会看传入文件并决定根据自己的规则和能力,用它做的事。

I don't think you can do this with a client-side command or setting. You would need to do something on the server so that it returns a content type of application/octet-stream for every file. Otherwise, the browser will look at the incoming file and decide what to do with it based on its own rules and capabilities.

如果你可以在你的服务器上执行此操作,请尝试内容处置头设置为附件;文件名= whatever.xyz

If you can do this on your server, try setting the "Content-Disposition" header to "attachment; filename=whatever.xyz"

这篇关于Asp.net MVC FileContentResult - prevent开口浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆