【问题标题】:Uncaught ReferenceError:LocalFileSystem is not defined未捕获的引用错误:未定义本地文件系统
【发布时间】:2016-05-24 19:45:01
【问题描述】:

我正在尝试了解我遇到的问题。 我正在通过 Cordova 为 Android 做一个应用程序。 我正在寻找解决方案,但实际上还没有找到。

首先,我遇到了未定义的“requestFileSystem”问题,但现在似乎已修复。 一旦我解决了这个问题,问题就会转移到另一个函数。

我基本上有一个按钮,它应该创建、保存(并可能显示)PDF:

<!--Button to generate PDF-->

        <div data-role="collapsible" data-inset="false" data-mini="true">
            <h3>Reports</h3>

            <div>
                <a href="javascript:pdfCreation()" class="button"data-role="button" id="PageUnitButtonGeneratePDIReport">Generate PDI Report</a>
                <!--<iframe width="100%" height="500px" id="pdfOutContainer"></iframe>-->
            </div>

        </div>

这是包含问题的我的 JS 代码:

function pdfCreation() {

    //FIRST GENERATE THE PDF DOCUMENT ON TABLET, DOESN'T SHOW
    console.log("generating pdf...");
    var doc = new jsPDF();
    doc.text(20, 20, 'HELLO!');
    doc.setFont("courier");
    doc.setFontType("normal");
    doc.text(20, 30, 'This is a PDF document generated using JSPDF.');
    doc.text(20, 50, 'YES, Inside of PhoneGap!');
    var pdfUriString = doc.output('datauristring');
    console.log(pdfUriString);
    var pdfOutContainer = jQuery("#pdfOutContainer");
    pdfOutContainer.attr("src", pdfUriString);

    //NEXT SAVE IT TO THE DEVICE'S LOCAL FILE SYSTEM
    console.log("file system...");
    //requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {

    document.addEventListener("deviceready", onDeviceReady, false);
        function onDeviceReady() {
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {

        console.log(fileSystem.name);
        console.log(fileSystem.root.name);
        console.log(fileSystem.root.fullPath);

        fileSystem.root.getFile("test.pdf", {create: true}, function(entry) {
            var fileEntry = entry;
            console.log(entry);

            entry.createWriter(function(writer) {
                writer.onwrite = function(evt) {
                    console.log("write success");
                };

                console.log("writing to file");
                writer.write( pdfOutput );
            },

            function(error) {
                console.log(error);
            });

        },

        function(error){
            console.log(error);
        });
    },

    function(event){
        console.log( evt.target.error.code );
    });
}

有问题的函数是pdfCreation。 我在这里添加 jdfiddle,以防万一它可能有用。

https://jsfiddle.net/antus2f0/

如果有人可以帮助我,那就太好了! 谢谢。

【问题讨论】:

    标签: javascript android cordova uncaught-exception


    【解决方案1】:

    稍微清理了您的代码。它缺少}。此外,如果您在 jsFiddle 中运行该函数,您将在该函数上收到 is not defined 错误,因为它会自动包装在 window.onload 函数中。

    这是工作代码:

    window.pdfCreation = function() {
    
      //FIRST GENERATE THE PDF DOCUMENT ON TABLET, DOESN'T SHOW
      console.log("generating pdf...");
      var doc = new jsPDF();
      doc.text(20, 20, 'HELLO!');
      doc.setFont("courier");
      doc.setFontType("normal");
      doc.text(20, 30, 'This is a PDF document generated using JSPDF.');
      doc.text(20, 50, 'YES, Inside of PhoneGap!');
      var pdfUriString = doc.output('datauristring');
      console.log(pdfUriString);
      var pdfOutContainer = jQuery("#pdfOutContainer");
      pdfOutContainer.attr("src", pdfUriString);
    
      //NEXT SAVE IT TO THE DEVICE'S LOCAL FILE SYSTEM
      console.log("file system...");
      //requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
    
      document.addEventListener("deviceready", onDeviceReady, false);
    
      function onDeviceReady() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
    
            console.log(fileSystem.name);
            console.log(fileSystem.root.name);
            console.log(fileSystem.root.fullPath);
    
            fileSystem.root.getFile("test.pdf", {
                create: true
              }, function(entry) {
                var fileEntry = entry;
                console.log(entry);
    
                entry.createWriter(function(writer) {
                    writer.onwrite = function(evt) {
                      console.log("write success");
                    };
    
                    console.log("writing to file");
                    writer.write(pdfOutput);
                  },
    
                  function(error) {
                    console.log(error);
                  });
    
              },
    
              function(error) {
                console.log(error);
              });
          },
          function(event) {
            console.log(evt.target.error.code);
          });
      }
    }
    

    【讨论】:

    • 感谢您的回复。我现在遇到的错误有点不同,但我已经在我做的几次测试中发现了它。 “未捕获的 ReferenceError:未定义 LocalFileSystem”
    【解决方案2】:

    围绕代码工作,我得到了以下可行的解决方案,希望将来可以帮助其他人。

    window.pdfCreation = function() {
    
            //FIRST GENERATE THE PDF DOCUMENT
            console.log("generating pdf...");
            var doc = new jsPDF();
    
            doc.setFont("TimesNewRoman");   
            doc.text(20, 10, 'HELLO!');
            doc.text(20, 30, 'This is a PDF summary.');    
    
    
            //PDF GENERATION ENDED
    
            var pdfOutput = doc.output();
            console.log( pdfOutput );
    
            //NEXT SAVE IT TO THE DEVICE'S LOCAL FILE SYSTEM
            console.log("file system...");
            alert("Creating PDF...Please wait a few seconds!");
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
    
                console.log(fileSystem.name);
                console.log(fileSystem.root.name);
                console.log(fileSystem.root.fullPath);
    
                fileSystem.root.getFile( $("#PageUnitPurchaserName").text() + $("#PageUnitUnitName").text() + ".pdf", {create: true}, function(entry) {
    
                    var fileEntry = entry;
                    console.log(entry);
    
                    entry.createWriter(function(writer) {
                        writer.onwrite = function(evt) {
                        console.log("write success");
                        alert("PDF successfully created!");
                    };
    
                    console.log("writing to file");
                        writer.write( pdfOutput );
                    }, function(error) {
                        console.log(error);
                    });
    
                }, function(error){
                    console.log(error);
                });
            },
    
            function(event){
                console.log( evt.target.error.code );
            });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-27
      • 2011-09-25
      • 2021-11-01
      • 2017-11-22
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多