保存< form:input type ="文件">的值与Spring MVC [英] Preserving value for <form:input type="file"> with Spring MVC

查看:262
本文介绍了保存< form:input type ="文件">的值与Spring MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < form:form ...> 

我有一个有两个输入的表单:一个字符串,一个文件。 ;
< form:input type =text....>
< form:input type =file....>
< / form:form>

如果验证在字符串输入上失败,则控制器返回相同的视图。发生这种情况时,字符串字段被保留,但所选文件丢失。



有没有办法在视图重新呈现时保留选中的文件?

I 认为答案是否定的 - 而且它是HTML文件输入的内在,而不是服务器端框架。如果有我缺少的东西。

解决方案

确实无法保存文件输入值。就像它在所有浏览器中实现一样。据我所知,这与潜在的安全风险有关,但我从来没有深入过。

尽管有类似的可能性。这是我们在我们的项目中做的:


  • 每个带有fileupload的表单都有一个UUID,只要输入无效就不会改变。

  • 我们使用FineUploader在这些编辑阶段中异步上传文件,这些文件以uuid作为文件夹名称上传到一个文件夹中。
  • 我们维护隐藏的输入(text)引用当前的上传文件

  • 如果没有bindingresult错误,我们在一个方法中处理这些文件并且完成我们删除上传文件夹>如果出现错误,我们保留uuid,并维护输入字段,所以我们不会丢失引用。



  • 现在这个工作很稳定,而且作为一个很好的副作用,控制器在某种程度上更容易,而且在我眼中也更清洁,因为我们不不再需要这些形式的多部分,而只需要处理上传的另一个控制器。



    编辑:
    - 您可能想要实现一个cronjob来清除被放弃的上传!


    I have a form with two inputs: one string, one file.

    <form:form ...>
      <form:input type="text" ....>
      <form:input type="file" ....>
    </form:form>
    

    If the validation fails on the string input, the controller routes back to this same view. When that happens, the string field is preserved but the selected file is lost.

    Is there a way to preserve the file selected when the view is re-rendered?

    I think the answer is no - and that it is intrinsic to the HTML file input, not the server-side framework. Asking in case there's something I'm missing.

    解决方案

    It is true that the file input value cannot be preserved. Thats just as it is implemented in all browsers. As far as i can tell it has to do with potential security risks but i never went deeper.

    There is a possibility to achieve something similar though. This is what we did in our project:

    • Each form with fileupload has a UUID that will not change as long as inputs are invalid.
    • We used FineUploader to asynchronosly upload files during these edit phase(s) which uploads to a folder with this uuid as foldername
    • We maintain hidden inputs (text) with references to the current uploads
    • If there are not bindingresult errors we process the files in a method and oncomplete we remove the upload folder
    • If there is an error we keep the uuid in place and maintain the input fields so we do not loose the references.

    This works stable now and as a nice side effect the controller was somehow easier and in my eyes also cleaner to write since we do not need multipart for these forms anymore and another controller just handling uploads.

    Edit: - you might want to implement a cronjob to clear abandonned uploads!

    这篇关于保存&lt; form:input type =&quot;文件&quot;&gt;的值与Spring MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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