欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

前端下载文件

程序员文章站 2022-05-13 09:50:22
...

//保存文件
saveData:function(){
var that = this
var name = that.fileleftname

 let difffile = that.difffile
 difffile = difffile.replace(/\"/g,"")
 difffile = difffile.replace(/</g,that.fileleftname1+"    ")
difffile = difffile.replace(/>/g,that.filerightname1+"    ")
  difffile = difffile.replace(/---/g,"----------------------------------------------------------------------------------------------------------------------------------------------------------")

console.log(difffile)
// 建立a标签
var elementA = document.createElement(‘a’);
//文件的称号为时候戳加文件名后缀
elementA.download = name+“文件差异” + “.txt”;
elementA.style.display = ‘none’;
//天生一个blob二进制数据,内容为json数据

var blob = new Blob([difffile], {type: “text/plain;charset=utf-8”});
console.log(URL.createObjectURL(blob))
//天生一个指向blob的URL地点,并赋值给a标签的href属性
elementA.href = URL.createObjectURL(blob);
document.body.appendChild(elementA);
elementA.click();
document.body.removeChild(elementA);
}

相关标签: 笔记 javascript