问题
I am using JavaScript with FileReader and readAsDataURL so I can convert images to base64 code and then store and display the base64 image code at a later time. I have a button that works fine when I select a file with it, but my question is: How can I use only a url to a local file (something like C:\folder\image.gif) (without using the file input button) and create an image object from that local image file url to use with the FileReader readAsDataURL functions? If this is not possible with FileReader, is there another way to automatically convert a local file to base64 code within the script?
回答1:
You can't. Allowing a web site to open arbitrary local files would be a serious, serious security concern. There would be nothing to prevent things like grabbing password files or other sensitive information without the user knowing. There have also been strict restrictions on the file input for that reason and to clearly communicate the intent.
来源:https://stackoverflow.com/questions/12764583/can-i-use-javascripts-filereader-on-a-local-file-if-i-know-the-location