gpt4 book ai didi

javascript - Uncaught ReferenceError :LocalFileSystem is not defined

转载 作者:行者123 更新时间:2023-12-03 06:52:56 26 4
gpt4 key购买 nike

我试图理解我所遇到的问题。我正在通过 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/

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

最佳答案

稍微清理一下你的代码。它缺少 }。此外,如果您在 jsFiddle 中运行该函数,您将在该函数上收到一个 未定义 错误,因为它自动包装在 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);
});
}
}

关于javascript - Uncaught ReferenceError :LocalFileSystem is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37422546/

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