验证错误后保留表单文件 [英] Keep form files after validation error

查看:46
本文介绍了验证错误后保留表单文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,我可以同时向后端发送几个(比如说 10 个)文件以及其他一些常规输入字段.

I have a form where I simultaneously send several (let's say 10) files to the backend, along with some other regular input fields.

当我在后端发现验证问题时,我再次显示表单并填写常规输入(下拉列表、文本输入等),但我无法填写文件字段,迫使用户从文件中选择文件再次目录.

When I find a validation problem in the backend I display the form again and fill the regular inputs (dropdowns, text inputs, etc.), but I cannot fill the file fields, forcing the user to select the files from the file directory again.

我想到的解决方案是发送文件的base64编码表示,并将它们放回表单中以防出现验证错误,但我想知道是否有更简单的方法.

The solution I think of is sending the base64 encoded representation of the files, and put them back in the form in case there is a validation error, but I would like to know if there is a simpler way.

推荐答案

无论您做什么,都不能跳过服务器端验证.出于安全原因(例如,有人发送原始请求).至于在出现错误(或简单的重新加载)时显示文件,我会将其作为 Base64 发送,但如果文件很大,则通过 AJAX 发送.

No matter what you do, you can not skip server-side validation. For security reasons (someone sending raw requests for example). As far as showing the files in case of error (or simple reloading) I would send it as Base64, but via AJAX if the files are large.

  1. 客户端验证:大多数验证(如果不是全部)都可以使用普通 JS 完成.无论是文件大小、内容类型……还是根据文件类型(图像、文档……)进行更具体的验证
  2. 服务器端验证:发送后,对文件和内容进行全面验证.
  3. 发回文件:如果出现错误,如果文件小,则将文件连同请求一起发送.如果大文件通过 AJAX 发送以避免阻塞并保持快速页面加载时间.
  1. Client-side validation: most validations (if not all) can be done using plain JS. Be it filesize, content type... and more specific validations according to filetypes (image, document...)
  2. Server-side validation: Once sent, fully validate the file and content.
  3. Send files back:In case of error, send the files with the request if small. If big send them via AJAX to avoid blocking and keep page loading times fast.

希望有帮助!

这篇关于验证错误后保留表单文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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