嗨,我尝试使用cordova-plugin-file在Phonegap中保存PDF数据字符串
示例:
%PDF-1.3
%ºß¬à
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/MediaBox [0 0 595.28 841.89]
/Contents 4 0 R
>>
endobj
4 0 obj
<</Length 8757>>
具有此功能的PDF文件:
savePDF("Storecheck"+surveyID+".pdf", pdfOutput)
function savePDF(fileName, fileData) {
console.log(fileData);
//function writeToFile(fileName, data) {
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(directoryEntry) {
directoryEntry.getFile(fileName, {
create: true
}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
// for real-world usage, you might consider passing a success callback
console.log('Speichern von "' + fileName + '"" abgeschlossen.');
};
fileWriter.onerror = function(e) {
// you could hook this up with our global error handler, or pass in an error callback
console.log('speichern fehlgeschlagen ' + e.toString());
};
var blob = new Blob([fileData], {
type: 'application/pdf'
});
fileWriter.write(blob);
}, );
}, );
}, );
}
我没有收到任何错误消息,但是文件没有出现。 另外,我想将PDF保存在“文档”中,而不是“ .file.dataDirectory”中
有人可以告诉我我做错了吗?
答案 0 :(得分:0)
根据the cordova-plugin-file documentaion,我更改了功能。 https://jsfiddle.net/frief/7umnwj0r/1/
我还添加了
<preference name="AndroidPersistentFileLocation" value="Internal" />
<preference name="AndroidPersistentFileLocation" value="Compatibility" />
<preference name="AndroidExtraFilesystems" value="sdcard,cache" />
<?xml version='1.0' encoding='utf-8'?>
<widget id="de.check" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
<name> check</name>
<description>
check
</description>
<author email="info@heins.de" href="http://ally.de">
Development
</author>
<preference name="AndroidPersistentFileLocation" value="Internal" />
<preference name="AndroidPersistentFileLocation" value="Compatibility" />
<preference name="AndroidExtraFilesystems" value="sdcard,cache" />
<preference name="iosPersistentFileLocation" value="Library" />
<preference name="iosPersistentFileLocation" value="Compatibility" />
<!-- Don't block any requests -->
<access origin="*" />
<content src="index.html" />
<plugin name="cordova-plugin-battery-status" spec="~1.2.2" />
<plugin name="cordova-plugin-camera" spec="~2.3.1" />
<plugin name="cordova-plugin-file" spec="~4.3.1" />
<plugin name="cordova-plugin-file-transfer" spec="~1.6.1" />
<plugin name="cordova-plugin-geolocation" spec="~2.4.1" />
<plugin name="cordova-plugin-vibration" spec="~2.1.3" />
<plugin name="cordova-plugin-statusbar" spec="~2.2.1" />
<plugin name="cordova-plugin-whitelist" source="npm"/>
<engine name="android" spec="^6.4.0" />
</widget>
现在我有一个问题,我总是收到无法创建我的文件的错误。 我认为这与访问权限有关,但我不清楚。
答案 1 :(得分:0)
哦,三天后我解决了
实际上,我只需要更改一行代码
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
LocalFileSystem.PERSISTENT 仅适用于Android WITH ROOT
LocalFileSystem.PERSISTENT 代表Android中的系统URL,其中包含您可以在文档中看到的内容:File System Layouts
我将其更改为 cordova.file.externalDataDirectory
在Phongap Desktop上,输出NULL 您必须构建应用才能正常工作
所以我想到了这个
savePDF("Storecheck.pdf", pdfOutput)
function savePDF(fileName, fileData) {
window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (dir) {
dir.getFile(fileName, {create: true, exclusive: false}, function (fileEntry) {
fileEntry.createWriter(function (writer) {
writer.onwrite = function (evt) {
console.log("write success");
alert(fileName+" wurde gespeichert unter " + cordova.file.externalDataDirectory );
};
console.log("writing to file");
writer.write(fileData);
})
}, function () {
console.log("ERROR SAVEFILE");
alert ("heute gibts keine PDF")
});
});
}