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.
-
Ideally you should not be able to read any file from the filesystem. That would be a security breach. If the file is being downloaded/ stored in the temporary FileSystem used by HTML5 then it is possible. – user3613129 Mar 13 '16 at 12:09
-
@user3613129 do you think I could somehow intercept a download to manage it with the fs api then? – Giacomo Lacava Mar 13 '16 at 12:30
-
I don't think so. Again if your extension would be able to intercept the download of a user, that would also be a security breach. FS API has a different use case. It is used to write/ read to/ from the Local filesystem, where the local filesystem means a temporary protected path created by the browser for these operations. Please go through the FS API for more information. – user3613129 Mar 13 '16 at 12:36
-
1@user3613129 Anything an extension can do can be seen as a "security breach". They have elevated privileges compared to the web code. – Xan Mar 13 '16 at 13:02
3 Answers
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}
.

- 664
- 10
- 18
-
Uhm, I tried this in firefox and it doesn't seem to work. The code just stalls as soon as i do `xhr.open("GET", "file:///Users/myuser/Downloads/myexistingfile", false)`. It works fine when the url is remote, so the call is correct. Can you list your `permissions` array? Maybe FF is more restrictive than Chrome... – Giacomo Lacava Nov 09 '17 at 19:01
-
Nevermind, it's because local files do not have the same events as remote ones. By attaching to `onreadystatechange` things work as expected. Great! – Giacomo Lacava Nov 09 '17 at 19:39
-
.. or not. I still cannot read the actual data after the request is complete, the response is always empty in firefox. Disappointing. – Giacomo Lacava Nov 09 '17 at 21:57
-
2You should not need more permissions than "downloads" and "file://*". As for the xhr request, be careful because what you retrieve is probably a Blob data. Here's my sample code for the request: `var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (this.readyState == 4) { file.blobData = this.response; } } xhr.open('GET', "file://" + file.filename); xhr.responseType = 'blob'; xhr.send();` – WhiteFangs Nov 10 '17 at 10:16
-
2I think it may be deliberately not supported in FireFox according to https://wiki.mozilla.org/WebExtensions/Filesystem#Accessing_file:.2F.2F_in_tabs_and_content_scripts – Steve Cadwallader Aug 08 '19 at 18:38
-
I've tried this and as many things as I can and am still getting an error "XMLHttpRequest at 'file:///Users/my_user/Downloads/downloaded_file' from origin 'http://localhost' has been blocked by CORS policy". I've given "permissions": ["downloads"], "host_permissions": ["file://*"], and "web_accessible_resources": [{"resources": ["file://*"],"matches": ["
"]}], in my Manifest file, Tried opening Chrome with --allow-file-access-from-files flag, And turned off my chrome security settings (Momentarily), And nothing helps this CORS issue. Can anyone help me resolve this? – Tyler Stone Jun 14 '23 at 06:52
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).

- 74,770
- 16
- 179
- 206
-
If I initiated the download myself with a separate explicit action via XHR (i.e. Right-click-> "download with MyExtension"), would I then be able to prompt the user to save it in the standard Downloads folder somehow? – Giacomo Lacava Mar 13 '16 at 13:20
-
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.

- 1
- 2