您好,我尝试使用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“中
谁能告诉我我哪里做错了?
发布于 2018-09-08 07:23:01
哦,耶,我三天后就解决了
实际上,我只需要修改一行代码
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {LocalFileSystem.PERSISTENT只能在带有根的安卓上运行
LocalFileSystem.PERSISTENT代表Android中的系统URL,您可以在文档中看到:File System Layouts
我把它改成了cordova.file.externalDataDirectory
在Phongap桌面上,为了运行,您必须构建应用程序,输出为空的
所以我提出了这个:
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")
});
});
}发布于 2018-09-08 00:05:59
根据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" />对于config.xml来说,它现在看起来像这样
<?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>
现在我的问题是,我总是得到错误,我的文件无法被创建。我认为这与访问权限有关,但我不知道确切的情况。
https://stackoverflow.com/questions/52202732
复制相似问题