Reading files from hard drive with javascript

回眸只為那壹抹淺笑 提交于 2019-12-24 14:57:36

问题


My application creates .xml files and stores them on the user's hard drive; there is a default folder that I set in the web.xml to store the files, let's say D:/temp. Now after I write the files I need to read them back with javascript, I am using a javascript library that has this function mxUtils.load('URL of the file') (this function returns the content of the file), the problem is that it is giving me an error Cross origin requests are only supported for HTTP, (I now it doesn't have anything to do with the function or the library) I think the problem is that you can't read local files because of some security issues. Anyone can advise me some solution? Thanks


回答1:


You cant access local filesystem using javascript. For accessing the file using javascript , you have to upload it to a server and access it using the files url.




回答2:


As stated, you cannot access a file on the filesystem strictly through Javascript. You could, however, use the input file type to upload the file to your server and then read it:

<input type="file" name="myfileinput">

Then, you can access it via the $_FILES global in PHP - other languages also provide this functionality through other means. Please note, again, that there is absolutely no way to access a file that is on someone's filesystem with Javascript without their consent (i.e. using the file input type). That would be a huge security risk - imagine going to a page and having it wipe your whole D:/ drive.




回答3:


It's best to expose your files via HTTP and use mxUtils.load("http://yoursite/static/yourfile.xml").

Search for static files on Apache HTTP Server HowTo. Setup Apache to serve your xml files, make sure that you can view xml file in browser and then use the same url in mxUtils.load call.



来源:https://stackoverflow.com/questions/20138001/reading-files-from-hard-drive-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!