上传本地文件,无需选择按钮 [英] Upload local file without choose button

查看:86
本文介绍了上传本地文件,无需选择按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于将文件上传到某个服务器的问题。我明白,由于安全问题,Web浏览器不允许JavaScript访问用户的文件目录。所以目前我的方法是添加一个选择文件按钮并手动选择要上传的文件。我在互联网上做了大量的其他替代方法的研究,但没有发现任何有用的东西。有没有任何可能的方法来自动选择文件的过程? (我只需要上传一个文件)就像改变网页浏览器的设置或者将这个文件附加在html或者其他任何方式的某个地方一样? 解决方案

我很抱歉地说,但我过去曾尝试过这种方式,并且由于安全原因,网络浏览器不允许这样做。即使您知道用户计算机上的文件名,浏览器也只会将一个文件对象附加到您的< input type =file/> 元素中通过网络浏览器的原生'文件浏览器'。虽然这是一个坏消息,但这是正确的答案。



安全问题是网络浏览器强制用户必须的规则通知并且启动附加文件的动作,以在他们正在查看的网页中形成元素。



这个动作只能由用户通过默认在< input type =file/> 元素上的'browse'按钮或者用javascript捕获的用户的文件拖放操作。


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?

解决方案

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.

这篇关于上传本地文件,无需选择按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆