Blob流视频下载
upload(){
let url = window.URL.createObjectURL(this.blob)
console.log('url',url);
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', '下载的文件名')
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
},
导出txt文件
<button onclick="expt()">导出txt</button>
function expt(){
savefiles('文件内容','文件名称.txt')
}
function savefiles(data,name){
var urlObject = window.URL || window.webkitURL || window;
var export_blob = new Blob([data]);
var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
save_link.href = urlObject.createObjectURL(export_blob);
save_link.download = name;
save_link.click();
}
window.open预览、打开、下载base64图片、pdf、文件
<button onclick="showOrDownload('data:image/jpeg;base64,4RK0RXhpZgAATU0AKgAAAAgADAEAAAMAAAA')">下载图片</button>
<button onclick="showOrDownload('data:application/pdf;base64,4AAQSkZJRgABAQAA')">打开pdf</button>
function showOrDownload(base64String) {
var fileHeader = base64String.slice(0, base64String.indexOf(";base64"));
var imageType = fileHeader.slice(fileHeader.indexOf(":") + 1, fileHeader.indexOf("/"));
if (imageType === 'image') {
this.base64StringToDownload(base64String);
return;
}
var fileType = fileHeader.slice(fileHeader.indexOf("/") + 1);
if (fileType === 'pdf') {
this.showPdf(base64String);
return;
}
this.base64StringToDownload(base64String);
}
function showImage(base64String) {
const img = new Image();
img.src = base64String;
const newWin = window.open("", "_blank");
newWin.document.write(img.outerHTML);
newWin.document.title = "附件";
newWin.document.close();
}
function showPdf(base64String) {
var pdfResult = base64String;
let pdfWindow = window.open("");
pdfWindow.document.write("<iframe width='100%' height='100%' src=" + pdfResult + "></iframe>");
pdfWindow.document.title = "附件"
pdfWindow.document.close();
}
function base64StringToBlob(base64String) {
var arr = base64String.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], {type: mime});
}
function base64StringToDownload(base64String) {
const id = Number(Math.random().toString().substr(3, 5) + Date.now()).toString(36);
let blob = this.base64StringToBlob(base64String);
let url = URL.createObjectURL(blob)
let save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
save_link.href = url
save_link.download = "附件" + id;
save_link.click();
}