I have a question regarding upload a file to some server. I understand that due to security issues, web browser does not allow javascript to access user's file directory. So currently my method is to add a choose file button and manually choose a file to upload. I did a lot of research on internet for other alternate approach, but did not find anything useful. Is there any possible way to automate the process of choosing the file? (I only need to upload one file) Like changing web browser setting or attach this file somewhere inside html or any other approach?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
I'm sorry to say but I have tried this in the past and the web-browser does not allow it to be done due to security. Even if you know the filename on the user's computer, the browser will only attach a file object to your <input type="file" />
element if it was selected via the web-browser's native 'file browser'. Although this is bad news, this is the right answer.
The security issue is that web-browsers enforce the rule that the user must be informed and also initiate the action of the file being attached to form elements in the web-page they are viewing.
This action can only be initiated by the user via the default 'browse' button on the <input type="file" />
element or a file drag/drop action by the user captured with javascript.