有没有办法将pdf文件从服务器保存到chrome打包的应用程序?
在我的 Chrome 打包应用程序中,我有这样的东西,
下载
当用户单击此超链接时,我应该能够将该 pdf 文件下载到我的 chrome 打包应用程序文件系统中。
最佳答案
下载 PDF 文件没有什么特别的。使用 XMLHttpRequest
下载文件,然后使用文件 API 将其写入沙盒文件,或者写入通过 chrome 获取其
.FileEntry
的外部文件.fileSystem.chooseEntry
下载后,您可以在 WebView 中显示 PDF,或者如果您首先使用 FileReader.readAsDataURL
将其转换为数据 URL,则可以提供在外部浏览器中打开它的链接。 (您不能将下载的文件引用为 file://
URL。)
(Chrome 应用不应称为“打包”应用,因为后者指的是现已过时的旧版应用技术。)
更新:将下载的 blob 保存到文件中:
// Save a blob in a FileEntry
// (e.g., from a call to chrome.fileSystem.chooseEntry)
function saveToEntry(blob, fileEntry) {
fileEntry.createWriter(
function(writer) {
writer.onerror = errorHandler; // you supply this
writer.truncate(0);
writer.onwriteend = function () {
writer.write(blob);
writer.onwriteend = function () {
// blob has been written
};
};
},
errorHandler // you supply this
);
}
关于google-chrome-app - 下载外部pdf文件到chrome打包应用程序的文件系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26910077/