"下载"代替“打开”一个文本文件 [英] "download" instead of "open" a text file

查看:102
本文介绍了"下载"代替“打开”一个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GWT App中,我想要一个按钮,点击后会按给定的URL下载文件。这是一个纯文本日志文件(不是html),后缀为.out。

我第一次尝试使用window.open()(同时尝试使用gwt和本地javascript window.open)

不幸的是,浏览器(试过chrome和firefox和ie)试图变得聪明,并且注意到这个文件是一个文本文件,因此显示内容在弹出窗口中,而不是下载。更糟糕的是,浏览器似乎将.out文件渲染为html,并将所有换行符和制表符(基本上是任何空格)混淆。



我无法控制提供.out文件的http服务器。所以我正在寻找一个客户端解决方案。



有人可以为我推荐一个解决方案吗?



谢谢。

解决方案

如果不修改来自服务器的头文件,这是不可能的。 b
$ b

现代浏览器支持< a> 标签中的下载属性,您可以从常规链接触发下载: https: //developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes



不幸的是, IE11不是现代浏览器


In an GWT App, I would like to have a button which when clicked will download a file at a given url. It's a plain text log file (not html) with a .out suffix.

My first attempt is using "window.open()" (tried both gwt and native javascript window.open)

Unfortunately, the browser (tried chrome and firefox and ie) is trying to be smart and notices that the file is a text file, and therefore shows the content in a popup, instead of downloading it. To make matters worse, it seems that the browser is rendering the .out file as if it's html, and messes up all the line breaks and tabs (basically any white spaces).

I'm not in control of the http server that serves the .out file. So I'm looking for a client side solution.

Can someone suggest a solution for me?

Thanks.

解决方案

It's not possible without modifying the headers coming from the server.

Modern browsers support the download attribute in <a> tags which allows you to trigger a download from a normal link: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes

Unfortunately, IE11 is not a "modern browser".

这篇关于&QUOT;下载&QUOT;代替“打开”一个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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