Download external pdf files to chrome packaged app's file system

只谈情不闲聊 提交于 2019-12-14 02:17:52

问题


Is there any way to save pdf files from server to chrome packaged app?

In my chrome packaged app, i have some thing like this,

Download

when user clicks on this hyper link, i should able download that pdf file into my chrome packaged app file system.


回答1:


There's nothing special about downloading PDF files. Use XMLHttpRequest to download a file, and then use the file APIs to either write it to a sandboxed file, or to an external file whose FileEntry you get with chrome.fileSystem.chooseEntry.

Once downloaded, you can display the PDF in a webview or provide a link to open it in an external browser if you first convert it to a data URL with FileReader.readAsDataURL. (You can't reference the downloaded file as file:// URL.)

(Chrome Apps should not be referred to as "packaged" apps, as the latter term refers to a now-obsolete legacy app technology.)

Update: To save the downloaded blob to a file:

// 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
    );
}


来源:https://stackoverflow.com/questions/26910077/download-external-pdf-files-to-chrome-packaged-apps-file-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!