简体   繁体   English

单击“提交”按钮时,保留<input type =“file”>字段中的值

[英]Retaining the value in the <input type=“file”> field when submit button is clicked

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 

}

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 can attempt some CSS wizardry to display the previously chosen file name maybe beside the file input box to hint the user. 编辑:本不可能这样做 - 但您可以尝试一些CSS向导来显示以前选择的文件名,可能在文件输入框旁边提示用户。 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. 如果您想尝试并且非常酷,您还可以尝试将浏览器的本机文件输入文本显示区域与另一个填充了先前文件名的div叠加。 But this will prevent clicking on the input area and so is user unfriendly. 但这会阻止点击输入区域,因此用户不友好。 Too much work, little reward. 太多的工作,很少的奖励。

出于安全目的,这不允许由任何脚本设置,由浏览器供应商作为readonly文件输入实现。

There is one way to do this. 有一种方法可以做到这一点。 Submit the form details for validation using an AJAX submission method so the user never really leaves the page or "submits" the form. 使用AJAX提交方法提交表单详细信息以进行验证,以便用户永远不会真正离开页面或“提交”表单。 That way you can validate the result server-side but the user still has all their values populated, including file inputs. 这样,您可以验证服务器端的结果,但用户仍然填充了所有值,包括文件输入。

As mentioned, input[type=file] is readonly. 如上所述, input[type=file]是只读的。 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! ......而且,在大多数情况下,它提供了更好的用户体验!

Check out jquery.validation or some other validation plugin for your favourite framework, or write one yourself. 查看jquery.validation或您喜欢的框架的其他验证插件,或自己编写一个。 Keep in mind that you should also validate on the server side. 请记住,您还应该在服务器端进行验证。

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执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM