Leo Jiang
Leo Jiang

Reputation: 26085

How do I download a file to the extension folder in a Chrome extension?

I want to use chrome.extension.getURL to load a local file that was downloaded. Since this can only access files in the extensions folder, how do I save files to the extensions folder?

I tried chrome.downloads.download, but this saves to the downloads folder by default.

Upvotes: 3

Views: 2449

Answers (1)

woxxom
woxxom

Reputation: 73566

You can download the file via XMLHttpRequest/$.get, store it as a string/blob in some local storage like WebSQL, IndexedDB or chrome.storage.local, then add it to a webpage as a data URI.

You can't save the file anywhere except under the downloads directory, see chrome.downloads API:

  • chrome.downloads.download:

    Absolute paths, empty paths, and paths containing back-references ".." will cause an error. onDeterminingFilename allows suggesting a filename [...]

  • chrome.downloads.onDeterminingFilename:

    a path relative to the user's default Downloads directory, possibly containing subdirectories. Absolute paths, empty paths, and paths containing back-references ".." will be ignored.

There are no other APIs to download a file.

Upvotes: 4

Related Questions