使用 PHP 或 JavaScript 提交表单失败后,您可以重新填充文件输入吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6842571/
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
Can you re-populate file inputs after failed form submission with PHP or JavaScript?
提问by diggersworld
Okay so here's the scenario:
好的,这里是场景:
User is presented with form that contains file inputs. User submits form. Form does not pass validation/verification for one reason or another. User is presented form with errors highlighted. File inputs are now blank.
用户将看到包含文件输入的表单。用户提交表单。出于某种原因,表单未通过验证/验证。向用户显示错误突出显示的表单。文件输入现在是空白的。
Question:
问题:
Is it possible to re-populate the file inputs with the paths to the files the user originally selected. A PHP solution would be ideal, but I'm also open to JavaScript solutions as well.
是否可以使用用户最初选择的文件的路径重新填充文件输入。PHP 解决方案将是理想的,但我也对 JavaScript 解决方案持开放态度。
回答by darkliquid
I think the short answer here is no. You can't repopulate file upload fields. However, you can work around it.
我认为这里的简短回答是否定的。您无法重新填充文件上传字段。但是,您可以解决它。
If a file has been selected and the form submitted, then you've already received the file. What you can do is keep a reference to the file on disk and put that in a hidden field and show a message to indicate to the user you still have their file uploaded so it does not need to be replaced/re-uploaded. When your form gets submitted again without a file, you can check for the hidden field value and use that to get your local copy of the file they uploaded in their last attempt.
如果已经选择了一个文件并提交了表单,那么您已经收到了该文件。您可以做的是在磁盘上保留对文件的引用并将其放在隐藏字段中并显示一条消息以向用户表明您仍然上传了他们的文件,因此不需要替换/重新上传。当您的表单在没有文件的情况下再次提交时,您可以检查隐藏字段值并使用它来获取他们上次上传的文件的本地副本。
The other way to do this is to either submit the form via ajax (using either flash or the iframe method for the file upload) or to do an ajax call to validate the form first, and then only submit as normal if valid (thus no form reload, no loss of file upload field data).
另一种方法是通过 ajax 提交表单(使用 flash 或 iframe 方法上传文件)或先调用 ajax 来验证表单,然后仅在有效时才正常提交(因此没有表单重新加载,文件上传字段数据不会丢失)。
回答by Somwang Souksavatd
I solved this problem by let user pick upload file as the last field before submit the form ^^
我通过让用户在提交表单之前选择上传文件作为最后一个字段来解决这个问题^^