Test browser: Version of Chrome: 52.0.2743.116
It is a simple javascript that is to open an image file from local like ‘C: 02.jpg’
Please give me any suitable suggestions.
12 Answers 12
Know this is kind of old, but see many questions like this.
We use Chrome a lot in the classroom and it is a must to working with local files.
What we have been using is "Web Server for Chrome". You start it up, choose the folder wishing to work with and go to URL (like 127.0.0.1:port you chose)
It is a simple server and cannot use PHP but for simple work, might be your solution:
Okay folks, I completely understand the security reasons behind this error message, but sometimes, we do need a workaround. and here’s mine. It uses ASP.Net (rather than JavaScript, which this question was based on) but it’ll hopefully be useful to someone.
Our in-house app has a webpage where users can create a list of shortcuts to useful files spread throughout our network. When they click on one of these shortcuts, we want to open these files. but of course, Chrome’s error prevents this.
This webpage uses AngularJS 1.x to list the various shortcuts.
The function itself is very simple.
And in my ASP.Net project, I added a Handler file called DownloadExternalFile.aspx which contained this code:
Now, when a user clicks on one of my Shortcut links, it calls the OpenAnExternalFile function, which opens this .ashx file, passing it the path+filename of the file we want to open.
This Handler code loads the file, then passes it’s contents back in the HTTP response.
And, job done, the webpage opens the external file.
Phew ! Again – there is a reason why Chrome throws this " Not allowed to load local resources " exception, so tread carefully with this. but I’m posting this code just to demonstrate that this is a fairly simple way around this limitation.
Just one last comment: the original question wanted to open the file " C: 02.jpg ". You can’t do this. Your website will sit on one server (with it’s own C: drive) and has no direct access to your user’s own C: drive. So the best you can do is use code like mine to access files somewhere on a network drive.
Пишу приложение под Chrome, сохраняю добавленные пользователем изображения в БД, точнее ссылки на них, чтобы потом пользователь мог продолжить работу с ними позже. Но изображения не отображаются в окне приложения.
Если ссылки доставать в чистом виде: "C:Imagesmyjpeg.jpg" Chrome выдает такой эксепшен: "Not allowed to load local resource: C:Imagesmyjpeg.jpg (если добавить file:///, ситуаций не изменится). Ок, получается, что надо ссылки превращать в File object, а потом в специальный URL object и потом отобразить, но почему-то этого не происходит. Ссылку он формирует, она прикрепляется корректно к img, но не отображается.
По идее такой код должен выдавать корректную ссылку для отображения в img, но я что-то упускаю или не правильно понимаю? Может быть объект файла как-то по другому надо создавать для createObjectURL()?
Comments
Copy link Quote reply
itsananderson commented May 8, 2015
When you uncomment this line in a newly generated project to use the main.jsbundle file, debugging in Chrome stops working.
When you "Debug in Chrome":
Of course, this is because the page is trying to load a file:// script. Totally expected, but it does make it painful to debug scenarios that might only occur when you load from the bundle.
I actually tweaked the packager code to work around this.
In packager/debugger.html , I updated the loadScript function to check for the file:// protocol, and proxy the request through the packager. e.g. http://localhost:8081/file://. .
Then in packager/packager.js I updated getDevToolsLauncher to check for /file:// in the path, and to serve that file.
Is this workaround something you’d be interested in me sending a PR for, or is the current behavior intentional?
Obviously the main drawback of my workaround is that it exposes a way to fetch arbitrary files from the machine. Considering the packager/debugger already allows any connected websocket clients to cause arbitrary code execution through executeApplicationScript , is that a concern?