Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

javascript - "download" instead of "open" a text file

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...