php 单击提交按钮时保留 <input type="file"> 字段中的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11426418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retaining the value in the <input type="file"> field when submit button is clicked
提问by Abida
I have got a <input type="file" >field along with other text fields in a form, When i try to upload any files using browse button and then click submit button ,the value in the input type= "file" field disappears , I would like the browsed value to remain in the <input type="file" >field if errors are present in other fields , is there any way i can retain the value that is browsed and for it to remain in the <input type="file" >field when submit button is clicked ,
我有一个<input type="file" >字段以及表单中的其他文本字段,当我尝试使用浏览按钮上传任何文件然后单击提交按钮时,输入类型 =“文件”字段中的值消失,我希望浏览的值<input type="file" >如果其他字段中存在错误,则保留在该字段中,有什么方法可以保留浏览的值并在<input type="file" >单击提交按钮时将其保留在该字段中,
<form action="form.php" method="post" enctype= multipart/form-data>
<input type="file" value="$file" name="file"/>
<input type="text" value="$line" name="line">
<input type="submit" name="btnsubmit">
</form>
if($_POST['btnsubmit'])
{
$line =$_POST['line'];
$file =$_FILES['file'] ['name'];
if($line)
{
//do something
//conditions for file check here
}
else
//error
}
回答by Aditya M P
It is not possible to do this. Browser security prevents you from pre-populating the File input field, so that websites cannot steal private files of their will without the user authorizing it first (by clicking "Browse..." and choosing a file).
这是不可能的。浏览器安全性可防止您预先填充文件输入字段,因此网站无法在未经用户授权的情况下窃取其私人文件(通过单击“浏览...”并选择文件)。
EDIT: It is not possible to do this natively- but you canattempt some CSS wizardry to display the previously chosen file name maybe beside the file input box to hint the user. If you want to try and be really cool, you can also attempt to overlay the browser's native file input text display area with another div that has the previous file name filled in it. But this will prevent clicking on the input area and so is user unfriendly. Too much work, little reward.
编辑:本机无法执行此操作- 但您可以尝试一些 CSS 魔法来显示先前选择的文件名,可能在文件输入框旁边以提示用户。如果您想尝试并变得非常酷,您还可以尝试用另一个填充了先前文件名的 div 覆盖浏览器的本机文件输入文本显示区域。但这会阻止点击输入区域,因此对用户不友好。工作太多,回报太少。
回答by Raab
This not allowed to be set by any script for security purpose, implemented by browser vendors as file input as readonly.
出于安全目的,这不允许由任何脚本设置,浏览器供应商将其作为文件输入实现为readonly.
回答by SpliFF
There is one way to do this. Submit the form details for validation using an AJAX submission methodso the user never really leaves the page or "submits" the form. That way you can validate the result server-side but the user still has all their values populated, including file inputs.
有一种方法可以做到这一点。使用AJAX 提交方法提交表单详细信息以进行验证,这样用户就不会真正离开页面或“提交”表单。这样您就可以在服务器端验证结果,但用户仍然填充了他们的所有值,包括文件输入。
回答by J?rgen
As mentioned, input[type=file]is readonly. By validating your input on the client side, you can prevent the submit to happen unless all fields are valid. ...and, in most cases, it provides a much better user experience!
如前所述,input[type=file]是只读的。通过在客户端验证您的输入,您可以防止提交,除非所有字段都有效。...而且,在大多数情况下,它提供了更好的用户体验!
Check out jquery.validation or some other validation plugin for your favourite framework, or write one yourself. Keep in mind that you should also validate on the server side.
查看 jquery.validation 或您最喜欢的框架的其他一些验证插件,或者自己编写一个。请记住,您还应该在服务器端进行验证。
By preventing the request, the file input will keep it's value until all fields are OK. If you need server side validation, you could also do this using ajax.
通过阻止请求,文件输入将保持其值,直到所有字段都正常。如果您需要服务器端验证,您也可以使用 ajax 执行此操作。

