使用 JavaScript 的浏览器下载文件提示
我想知道是否有任何方法可以使用JavaScript实现浏览器的下载文件提示。
我的原因 - 用户将文件上传到无法从网络服务器访问的本地文件服务器。 换句话说,两者将位于不同的域中!
例如,假设网站托管在 www.xyz.com
上,但文件将驻留在地址为 \\10.10.10.01\Files\file.txt
的本地文件服务器上。 我如何使用 ActiveX 和 VBscript 将文件上传/传输到本地文件服务器! (不要问:-)
所以我将本地文件路径存储在我的数据库中并将该数据绑定到网格。 当用户单击该链接时,文件将在一个窗口中打开(使用 JavaScript)。
问题是某些文件类型(如文本、jpg、pdf 等)在浏览器窗口中打开。 如何使用客户端脚本实现 content-type
或 content-disposition
? 这可能吗?
编辑: 本地文件服务器有一个保存文件的windows共享文件夹。
I was wondering if there was any method to implement browser's download file prompt using JavaScript.
My reason - well users will be uploading files to a local fileserver which cannot be accessed from the webserver. In other words, both will be on different domains!
For example, let’s say websites hosted on www.xyz.com
, but files would reside on local file server with address like \\10.10.10.01\Files\file.txt
. How am I uploading/transferring file to local fileserver... using ActiveX and VBscript! (don’t ask :-)
So I am storing local file path in my database and binding that data to a grid. When the user clicks on that link, the file opens in a window (using JavaScript).
Problem is certain file types like text, jpg, pdf, etc. open inside browser window. How would I be able to implement content-type
or content-disposition
using client side scripting? Is that even possible?
EDIT:
the local file server has a window's shared folder on which the files are saved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
“content-disposition:attachment”几乎是强制执行此操作的唯一方法,并且必须在响应标头中设置。
"content-disposition: attachment" is pretty much the only way to force that, and this MUST be set in the response header.
如果文件托管在 Web 服务器上(如您的示例所示),您可以执行以下操作:
.. 浏览器将确定如何处理该文件。 这对于大多数文件(例如 .xls、.csv 等)都非常有效,但请记住,这并不是完全可靠的,因为用户的 MIME 处理程序设置将决定如何处理该文件...即,如果是.txt 文件很可能只会显示在浏览器中,并且不会出现“文件下载”对话框。
If the file is hosted on a web server like in your example, you can do:
.. and the browser will figure out what to do with the file. This works great for most files, such as .xls, .csv, etc, but keep in mind that this isn't full-proof because the user's MIME handler settings will determine what to do with the file... i.e. if it is a .txt file it will most likely just be displayed in the browser and will not be given a "file download" dialogue box.
自 2015 年 8 月起,将“下载”属性添加到您的标记即可实现您正在寻找的行为,至少在 Chrome 中是这样。
As of August 2015, adding the "download" attribute to your tag enables the behavior you're looking for, at least in Chrome.
您可以尝试使用
type="application/octet-stream"
的普通超链接。 似乎在 FF 中有效,但 IE 和 Opera 忽略该属性。You could try using a plain hyperlink with
type="application/octet-stream"
. Seems to work in FF, but IE and Opera ignore the attribute.