【问题标题】:write blob to a text file?将blob写入文本文件?
【发布时间】:2014-09-09 03:34:54
【问题描述】:

我能够在新窗口中看到必须写入文件的文本。使用以下代码。但我想将其保存到本地的文本文件中。尝试使用 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();

};

尝试像 saveAs(blob ,test.txt) 一样出现错误.. saveAs 未定义。

【问题讨论】:

    标签: javascript three.js html5-canvas


    【解决方案1】:

    很遗憾,目前任何浏览器都不支持saveAs()

    您可以通过以下代码快速检查这一点

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

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

    这可以通过添加以下代码行来代替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

    【讨论】:

      【解决方案2】:

      我在这个问题上苦苦挣扎,但对我有用的是强制您的 html 仅使用受支持的浏览器版本 - 由于某些愚蠢的原因,IE 会降至低于支持的版本,所以我使用...

      <meta http-equiv="X-UA-Compatible" content="IE=10" />
      

      ...在 HTML 页面的标题中,并在函数外部但仍在脚本标签内启动输出变量(稍后将显示),例如:

      <script src="FileSaver.js"></script>
      <script>
      
      var output; 
      
      function exportGeometry ( ) {
      
          var output=''; ...
      

      这是假设您使用脚本 FileSaver.js,如果没有,您可以在以下位置了解它: https://github.com/eligrey/FileSaver.js/

      希望这会有所帮助,祝你好运!

      【讨论】:

      • 如果没有对 filesaver.js 是什么、在哪里获得它以及为什么需要它的一些评论,这个答案并不是很有帮助。
      • 抱歉,我添加了一个链接,解释它的作用、使用演示和所需文件的下载 - 我使用它将变量转换为 blob,然后再转换为文件以供下载。跨度>
      猜你喜欢
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      • 1970-01-01
      • 2013-04-04
      • 2011-03-23
      • 2019-11-27
      • 1970-01-01
      相关资源
      最近更新 更多