Sagar
Sagar

Reputation: 647

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

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.

Upvotes: 2

Views: 1651

Answers (1)

Marc Rochkind
Marc Rochkind

Reputation: 3740

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

Upvotes: 2

Related Questions