保留< input type ="文件">中的值点击提交按钮时字段 [英] Retaining the value in the <input type="file"> field when submit button is clicked

查看:77
本文介绍了保留< input type ="文件">中的值点击提交按钮时字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个< input type =file> 字段以及表单中的其他文本字段,当我尝试使用浏览按钮上传任何文件时然后点击提交按钮,input type =file字段中的值消失,我希望浏览的值保留在< input type =file> 字段,如果错误出现在其他字段中,有什么办法可以保留所浏览的值,并保留在< input type =file> 字段,

 < form action =form.phpmethod =postenctype = multipart / form-data> 

< input type =filevalue =$ filename =file/>
< input type =textvalue =$ linename =line>
< input type =submitname =btnsubmit>

< / form>

if($ _ POST ['btnsubmit'])
{
$ line = $ _ POST ['line'];
$ file = $ _ FILES ['file'] ['name'];

if($ line)
{
//做某事
//文件检查条件

}
其他
//错误

}


解决方案

这是不可能的。浏览器安全性可以防止您预先填充文件输入字段,以便网站无法窃取他们遗嘱的私人文件,而无需用户先授权(通过单击浏览...并选择文件)。



编辑:原生 不可能 文件输入框来提示用户。如果您想尝试并且非常酷,您还可以尝试将浏览器的本机文件输入文本显示区域与其中已填充了先前文件名的另一个div叠加。但是这会阻止点击输入区域,用户不友好。工作太多,奖励少。

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 ,

<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. 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.

这篇关于保留&lt; input type =&quot;文件&quot;&gt;中的值点击提交按钮时字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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