简体   繁体   English

使用纯 javascript 创建下载提示

[英]Creating download prompt using purely javascript

I have some text data (say var a = 'Hello World From Javascript'; )in javascript variable in current window.我在当前窗口的 javascript 变量中有一些文本数据(比如var a = 'Hello World From Javascript'; )。 I want to do the following through javascript-我想通过javascript执行以下操作-

1. open a new window and write the text data to the window.
2. set the content type to text/plain.
3. set the content-disposition to attachment, so that download prompt comes.
4. user downloads the text data as a text file and saves it to his local disk.

is this all possible through javascript?这一切都可以通过javascript实现吗?

I know we can make ajax calls to server or redirect but in this case instead of following above steps.我知道我们可以对服务器进行 ajax 调用或重定向,但在这种情况下,而不是遵循上述步骤。 But in this case, these workarounds are not adaptable.但在这种情况下,这些变通办法是不适用的。

you can do that using JS & HTML5 features.你可以使用 JS 和 HTML5 特性来做到这一点。 Please find below a sample code.请在下面找到示例代码。

        var fileParts = ['Hello World From Javascript'];

        // Create a blob object.
        var bb = new Blob(fileParts,{type : 'text/plain'});

        // Create a blob url for this. 
        var dnlnk = window.URL.createObjectURL(bb);

        var currentLnk = $('#blobFl').attr('href');

        // blobFl is the id of the anchor tag through which the download will be triggered.

        $('#blobFl').attr('href',dnlnk);
        $('#blobFl').attr('download','helloworld.txt');

        // For some reason trigger from jquery dint work for me.
        document.getElementById('blobFl').click();

Triggering a file download without any server request在没有任何服务器请求的情况下触发文件下载

Unfortunately this is not something you can do with normal browser capabilities.不幸的是,这不是您可以使用普通浏览器功能完成的事情。 Something like flash or a browser-specific plugin will get you what you need, but security limitations within javascript will not let you download arbitrary data created within the browser.像 flash 或特定于浏览器的插件之类的东西可以满足您的需求,但是 javascript 中的安全限制不会让您下载在浏览器中创建的任意数据。

Also the 'data' url is not supported across all browser/version combinations.此外,并非所有浏览器/版本组合都支持“数据”网址。 I am not sure if your users are constrained on what browser they are using or not but that may limit what you can do with that solution.我不确定您的用户是否受到他们使用的浏览器的限制,但这可能会限制您使用该解决方案的功能。

Source: Triggering a file download without any server request来源: 在没有任何服务器请求的情况下触发文件下载

如果您已经在服务器上拥有该文件(我进行了 ajax 调用以在服务器上生成并保存 PDF) - 您可以这样做

window.location.replace(fileUrl);

No, Content-Disposition is a response header, it has to come from the server.不,Content-Disposition 是一个响应头,它必须来自服务器。 I think you could do it with Flash but I wouldn't recommend it.我认为你可以用 Flash 来做,但我不推荐它。

Here's a clean, pure js version of @Rajagopalan Srinivasan's answer :这是@Rajagopalan Srinivasan 答案的干净、纯 js 版本:

var fileParts = ["Hello World From Javascript"];
// The anchor tag to use.
const blobLink = document.getElementById("blobLink");
// Create a blob object.
var blob = new Blob(fileParts, { type: "text/plain" });

// Create a blob url for this.
var blobUrl = window.URL.createObjectURL(blob);

blobLink.setAttribute("href", blobUrl);
blobLink.setAttribute("download", "helloworld.txt");
blobLink.click();
<a id="blobLink">Download</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM