“下载"而不是“打开"一个文本文件 [英] "download" instead of "open" a text file

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

问题描述

在 GWT 应用程序中,我想要一个按钮,单击该按钮后将在给定的 url 处下载文件.这是一个带有 .out 后缀的纯文本日志文件(不是 html).

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.

我的第一次尝试是使用window.open()"(尝试了 gwt 和原生 javascript window.open)

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

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

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).

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

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?

谢谢.

推荐答案

不修改来自服务器的标题是不可能的.

现代浏览器支持 <a> 标签中的 download 属性,它允许您从普通链接触发下载:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#属性

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

很遗憾,IE11 不是现代浏览器".

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

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