用于下载字符串的 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21893463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:55:07  来源:igfitidea点击:

Javascript to Download String

javascript

提问by Joe Bergevin

Trying to initiate a browser download in Javascript, but the data I want to be downloaded is in a string, not a file. I know if it were a file, the following would do it:

尝试在 Javascript 中启动浏览器下载,但我要下载的数据是字符串,而不是文件。我知道如果它是一个文件,下面会做:

window.location.href = '/filepath/file.csv';

How can I get this same effect, only with a string (with csv data), not a file that already exists on the server?

我怎样才能得到同样的效果,只有一个字符串(带有 csv 数据),而不是服务器上已经存在的文件?

回答by dandavis

using my handy downloader:

使用我方便的下载器:

<script src="http://danml.com/js/download.js"></script>
<script>download("hello world", "hello.txt", "text/plain")</script>

you can do it without a library as well, though my "lib" isn't very big and supports older FF+CH and IE10:

你也可以在没有库的情况下做到这一点,尽管我的“lib”不是很大并且支持旧的 FF+CH 和 IE10:

<a id=dl download="file.txt">Download</a>
<script>
content=prompt("enter contents");
dl.href="data:text/plain,"+encodeURIComponent(content);
dl.click();
</script>

EDIT: the linked script now supports window.URL.createObjectURL() for downloading files that were too big using dataURLs. I don't know the new limit, but 10mb works just file, whereas ~2mb is a limit for many dataURL ( window.open/A[download] - based ) solutions3

编辑:链接的脚本现在支持 window.URL.createObjectURL() 来下载使用 dataURLs 过大的文件。我不知道新的限制,但 10mb 仅适用于文件,而 ~2mb 是许多 dataURL(基于 window.open/A[download])解决方案的限制3

回答by malkassem

Below is a function I have writen in the past to handle such behavior (it may require some tweaking):

下面是我过去编写的用于处理此类行为的函数(可能需要进行一些调整):

var downloadFile = function (filename, dataValue) {
    window.URL = window.webkitURL || window.URL;
    window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder;

    var prevLink = output.querySelector('a');
    if (prevLink) {
        window.URL.revokeObjectURL(prevLink.href);
        output.innerHTML = '';
    }

    var a = document.createElement('a');
    a.download = '" + filename + @".csv';

    if (BlobBuilder == undefined) {
        var bb = new Blob([dataValue], { 'type': MIME_TYPE });
        a.href = window.URL.createObjectURL(bb);
    }
    else {
        var bb = new BlobBuilder();
        bb.append(dataValue);
        a.href = window.URL.createObjectURL(bb.getBlob(MIME_TYPE));
    }

    a.textContent = 'Download ready';

    a.dataset.downloadurl = [MIME_TYPE, a.download, a.href].join(':');
    a.draggable = true; // Don't really need, but good practice.
    a.classList.add('dragout');

    output.appendChild(a);

    a.onclick = function (e) {
        if ('disabled' in this.dataset) {
            return false;
        }
    };
};