google-chrome-app - 下载外部pdf文件到chrome打包应用程序的文件系统

标签 google-chrome-app

有没有办法将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/

相关文章:

javascript - 我可以从 Chrome 打包应用程序的后台页面调用变量或函数吗?

javascript - chrome.app.window.create 隐身窗口?

cookies - 两个 ChromeApp WebView 是否可以拥有不同的 cookie 集?

css - Chrome 应用中的自定义滚动条

javascript - 后台脚本的 sendResponse 中数组数据丢失

google-chrome-app - 将 Chrome 打包应用链接到 Google Play 游戏

javascript - 如何在Web Worker中访问Chrome API?

google-chrome-app - 透明 ChromeOS 窗口

javascript - 如何在 Chrome 应用程序中包含 jQuery?

javascript - Chrome 应用程序 eval()