gpt4 book ai didi

javascript - 将 blob 写入文本文件?

转载 作者:行者123 更新时间:2023-11-30 06:53:30 28 4
gpt4 key购买 nike

我能够在新窗口中看到必须写入文件的文本。使用以下代码。但我想将它保存到本地文本文件。尝试使用 saveAsmsSaveBlob 它给出了一个错误。

window.navigator.msSaveBlob(blob, 'msSaveBlob_testFile.txt');

var blob = new Blob([output]), {type: "text/plain;charset=utf-8"}); saveAs(blob, "thing.txt");

function exportGeometry ( ) {
var output=[];
output.push("//TLR:Format:Vishama Creations:v1.0\n//Pattern_no,Pattern_NumLines,FrstPointIndx,PointName,aX,aY,aZ,bX,bY,bZ,SecondPointIndex,PointName,aX,aY,aZ,bX,bY,bZ\nL\n");
output.push(fpatternIndex);
output.push(fpatternLineCount);
output.push(fpatternPointCount);
for(var b=0;b<fpatternLineCount;b++)
{
output.push(ffirstPtIndx[b]);
output.push(fpatternPointName[ffirstPtIndx[b]]);
output.push(fpatternPoint[ffirstPtIndx[b]].x/300);
output.push(fpatternPoint[ffirstPtIndx[b]].y/300);
output.push(fpatternPoint[ffirstPtIndx[b]].z/300);

output.push(fsecondPtIndx[b]);
output.push(fpatternPointName[fsecondPtIndx[b]]);
output.push(fpatternPoint[fsecondPtIndx[b]].x/300);
output.push(fpatternPoint[fsecondPtIndx[b]].y/300);
output.push(fpatternPoint[fsecondPtIndx[b]].z/300);
}
output.push("\nL\n");
output.push(bpatternIndex);
output.push(bpatternLineCount);
output.push(bpatternPointCount);
for(var b=0;b<bpatternLineCount;b++)
{
output.push(bfirstPtIndx[b]);
output.push(bpatternPointName[bfirstPtIndx[b]]);
output.push(bpatternPoint[bfirstPtIndx[b]].x/300);
output.push(bpatternPoint[bfirstPtIndx[b]].y/300);
output.push(bpatternPoint[bfirstPtIndx[b]].z/300);

output.push(bsecondPtIndx[b]);
output.push(bpatternPointName[bsecondPtIndx[b]]);
output.push(bpatternPoint[bsecondPtIndx[b]].x/300);
output.push(bpatternPoint[bsecondPtIndx[b]].y/300);
output.push(bpatternPoint[bsecondPtIndx[b]].z/300);
}


var blob = new Blob(([output]), {type: "text/plain;charset=utf-8"});

var objectURL = URL.createObjectURL( blob);
window.open( objectURL, '_blank' );
window.focus();

};

tried like saveAs(blob ,test.txt) rises an error.. saveAs not defined.

最佳答案

不幸的是 saveAs()当前不支持任何浏览器。

你可以通过下面的代码快速检查一下

if (window.saveAs) {
console.log("saveAs supported");
}else{
console.log("saveAs not supported");
}

现在回到你的问题,你已经成功创建了你可以在新窗口中看到的文本文件,你只需要创建一个 anchor 元素(<a></a>)并模拟用户点击开始下载在浏览器中。

这可以通过添加以下代码行来代替 window.open( objectURL, '_blank' ); window.focus(); 来完成

附加代码

var a = document.createElement('a');
a.download = "test.txt";
a.href = url;
a.click();// you can use a.onclick(); if the former fails

这是一个

JSFiddle

关于javascript - 将 blob 写入文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24817507/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com