验证失败后恢复输入类型=文件的值 [英] Restoring the value of a input type=file after failed validation

查看:91
本文介绍了验证失败后恢复输入类型=文件的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个处理文件的输入表单。 javascript验证非常适合检查以确保一切正常,但有些事情需要PHP来检查(如文件的MIME类型),有时它会被拒绝。



问题是,当我将用户发回表单时,我可以重新填充它们原来输入的所有数据,除了具有文件类型的输入。 Firefox不提供绝对文件路径,所以我不能将文件路径复制到输入中。



我可以如何重新填充输入类型=文件表单元素?

解决方案

您无法填充文件输入,这样做会是一个严重的安全问题。 b
$ b

取而代之的是,将上传的内容存储在服务器上,并存储一个可用于以隐藏输入引用它的标识。

在达到特定年龄时自动清理文件,并为用户提供改变想要上传文件的方法(例如,复选框(对于存储在服务器上的每个文件进行上传)

I have a form with several inputs that deal with files. The javascript validation is pretty good at checking to make sure that everything is good, but some things require PHP to inspect (like the file's mime type), and sometimes it will get rejected.

The problem is, when I send the user back to the form, I can repopulate all the data that they had originally input, except the inputs with the type of file. Firefox doesn't provide the absolute file path so I can't just copy in a file path to the input.

What can I do to repopulate the input type=file form elements?

解决方案

You cannot populate the file input, to do so would be a serious security problem.

Instead, store the uploaded content on the server and store an id that you can use to reference it in a hidden input.

Clean the files up automatically after they reach a certain age, and provide a means for the user to change their mind about what file they want to upload (e.g. a checkbox (checked by default) for each file being stored on the server for upload)

这篇关于验证失败后恢复输入类型=文件的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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