JavaScript创建下载文件函数

原创
小哥 3年前 (2022-10-17) 阅读数 105 #CSS教程
文章标签 JavaScript
/**
 * 生成下载
 * @param {string} fileName 文件名
 * @param {string} fileType 文件后缀名称文件扩展名
 * @param {string} content 文件内容
 */
makeDownload(fileName, fileType, content) {
    var pom = document.createElement(a);
    pom.setAttribute(href, data:text/plain;charset=utf-8, + encodeURIComponent(content));
    pom.setAttribute(download, ${fileName}.${fileType});
    if (document.createEvent) {
        var event = document.createEvent(MouseEvents);
        event.initEvent(click, true, true);
        pom.dispatchEvent(event);
    } else {
        pom.click();
    }
}
版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门