Giacomo Lacava
Giacomo Lacava

Reputation: 1833

Accessing file data after download in Chrome...?

I'm writing an extension that would ideally read files after they are downloaded (through the normal download process). Is that possible? I can get the filename through chrome.downloads operations, but I can't find ways of reading the actual bytes. I don't need to write or move them, just read.

Upvotes: 9

Views: 6102

Answers (3)

pabrown
pabrown

Reputation: 1

WhiteFangs' solution worked for me. HOWEVER, I should add the caveat that the XMLHttpRequest using the file protocol only worked for me when I initiated it from the background script. I got errors when I tried it from a content script or from the javascript environment of my browser action.

Upvotes: 0

WhiteFangs
WhiteFangs

Reputation: 704

I struggled with the exact same problem and finally found an easy workaround to read the content of downloaded files from a Chrome extension.

You just need to add a permission to file://*in your manifest file and once you have your file's path in the system (with chrome.downloads.search in the filename property), make a GET XMLHttpRequest to the url file://{filepath}.

Upvotes: 10

Xan
Xan

Reputation: 77591

No, the Downloads API does not provide a way to access the file contents.

The closest for interception would be WebRequest API, but at the moment that doesn't allow response access either.

The most you can do is to catch a request with webRequest, cancel it and try to XHR it yourself, storing the result in some temporary place like the mentioned FileSystem API. This would be a terrible UX though (the original request fails inexplicably) and may not work in all cases (as XHR can't set all the headers to fully replicate a request).

Upvotes: 2

Related Questions