在 ASP.NET MVC 中验证失败后如何保留输入类型=文件字段值? [英] How to keep input type=file field value after failed validation in ASP.NET MVC?

查看:25
本文介绍了在 ASP.NET MVC 中验证失败后如何保留输入类型=文件字段值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我制作的 MVC 应用程序中有一个简单的表单.它包含一个文件字段,以便用户可以上传图像.一切都很好.

I've got a simple form in an MVC app I've made. It contains a file field so users can upload an image. It all works great.

问题是,如果表单提交验证失败,文件字段的内容将丢失(其他字段仍然填充,感谢 HtmlHelpers!).如何在验证失败后保持填充文件字段?

Problem is, if the form submit fails validation the content of the file field is lost (other fields remain populated, thx HtmlHelpers!). How do I keep the file field populated after a failed validation?

TIA!

推荐答案

浏览器之所以如此设计,是因为存在安全风险.无法在 HTML 源代码或 Javascript 中设置文件输入框的值.否则恶意脚本可能会在用户不注意的情况下窃取一些私人文件.

Browsers are designed in such way because of security risks. It's impossible to set value of file input box in HTML source or by Javascript. Otherwise malicious script could steal some private file without user attention.

有关于该主题的一个有趣的信息.

这篇关于在 ASP.NET MVC 中验证失败后如何保留输入类型=文件字段值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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