2

Is it possible for a chrome extension to access a pdf file dragged and dropped onto a Chrome. The url would appear as something like the following url. It would be a local file. I think due to security reason, it is most probably not possible but just want to double check. thanks

file:///Users/user1/Documents/example.pdf

I have read the following Stack Overflow threads.
1) Open local files(file://) using Chrome.
2) This one is the closest but not quite the same question.
Is it possible to emulate the "--allow-file-access-from-files" flag using a chrome extension?.
3) Can Google Chrome open local links?

Jun
  • 2,942
  • 5
  • 28
  • 50
  • The bigger problem will be to obtain the full path to the dropped file. No browser will give you access to it. But assuming you get the path anyways, the only way to read it would be to use a native extenion (chrome app)... – Manuel Otto Sep 07 '18 at 18:28
  • You can easily access the contents of a drag'n'dropped local file in the `drop` event handler via event.dataTransfer and FileReader API. There are many examples you can find. I'm assuming the file is dropped onto your extension page. Otherwise you need to enable file access checkbox in the detailed view of the extension on chrome://extensions page and you'll be able to access any local file that's accessible to the currently logged user. – wOxxOm Sep 07 '18 at 18:36
  • 1
    @wOxxOm, it is a file dragged and dropped onto a chrome browser, not onto my extension page. Yea, I have the 'Allow access to file URLs' checcked. How can I access the local file content then? What api is that? – Jun Sep 07 '18 at 18:53
  • 1
    XMLHttpRequest or fetch with a file:// URL. – wOxxOm Sep 07 '18 at 19:11
  • 1
    See also [Firefox WebExtensions, get local files content by path](https://stackoverflow.com/a/44516256) – wOxxOm Sep 07 '18 at 19:17
  • @wOxxOm I see. It is like accessing user's local system. If this is doable, isn't it dangerous as anyone can access the user's file system provided the path to the file. So now, I need to figure out how to get the url, is that possible according to Manuel's comment? – Jun Sep 07 '18 at 20:02
  • 1
    It is doable. It is dangerous. The file URL will be in the tab's URL so you can trivially get it using chrome.tabs API. – wOxxOm Sep 08 '18 at 04:11

0 Answers0