使用JS更改默认文件名下载?

时间:2018-10-17 14:08:20

标签: javascript button download filenames

我将如何更改客户端将要在网站上下载的剪辑的名称?当前下载的每个视频剪辑都是默认名称,我该如何对其进行自定义?

下面是下载按钮的功能。

// Function to generate the Download button
VideoPlayer.prototype.initDownload = function () {

    var downloadBtn         = $("button.download"),
        downloadToolTipCls  = "download_tooltip",
        sources             = {},
        downloadToopTip,
        sourcesLength = 0,
        sourcesKeys;


    // Add each source
    if (typeof (firstSrc = this.$video.attr("src")) !== "undefined") {
        sources[firstSrc] = this.$video.attr("type");
    }

    this.$video.find("source").each(function () {
        var $this = $(this);
        sources[$this.attr("src")] = $this.attr("type");
    });

    sourcesKeys = Object.keys(sources);
    sourcesLength = sourcesKeys.length;
    if (sourcesLength === 1) {
        downloadBtn.wrap("<a href=\"" + sourcesKeys[0] + "\" download />");
    } else if (sourcesLength > 1) {
        downloadBtn.after("<span class=\"" + downloadToolTipCls + "\" />");
        downloadToopTip = $("." + downloadToolTipCls);

        $.each(sources, function (source, type) {
            downloadToopTip.append("<a href=\"" + source + "\" download> Type " + type + "</a>");
        });

        downloadBtn.click(function () {
            downloadToopTip.toggle();
        });
    }
};

1 个答案:

答案 0 :(得分:0)

在下载属性download=name 中为文件添加所需的名称