从 Spring MVC 中的自定义表单标签上传文件
上下文
我们应用程序的管理员端部分要求用户编辑各种类型的内容,其中涉及使用富文本编辑器或使用文件生成“客户端”用户可以看到的内容的应用程序。它是一种特定于域的 CMS lite。
由于此“内容”可以在应用程序的各个部分中使用,因此它作为单独的关系包含在我们的一些域实体中。我们决定创建自己的标签库,定义一些表单字段,当管理员编辑包含一段内容的实体时,这些表单字段可用于编辑此内容。
问题
我们希望能够做以下事情。
<form:form modelAttribute=...>
<olo:content-editor path="content"/>
<!-- Other form fields for this entity -->
<form:.../>
<form:.../>
</form:form>
然后,olo:content-editor
标记根据需要的内容类型生成许多表单字段。这意味着它可能(或者,根据内容的类型,可能不会!)生成 filebased-content
标记,其中包含:
<input type="file" name="file/>
可用于替换与基于文件的内容关联的文件。
问题是 Spring 文档表明文件上传需要表单具有 enctype 来定义它正在发送多部分表单数据。由于文件上传是标签的一部分,而不是表单本身,我们发现这是不可取的。我们希望能够在表单中使用 olo:content-editor
标记,而无需更改表单 enctype
属性。这可能吗?
可能的解决方案
我们可以想到两种可以解决我们问题的客户端技巧,但这两种解决方案似乎都相当丑陋:
- 在基于文件的内容标记中包含一个脚本,该脚本在加载时会更改
enctype
形式,因此它始终设置为适当的类型。 (非常丑陋。) - 将文件数据作为常规隐藏表单字段提交,其中数据是使用 HTML5 文件 API 设置的(管理员使用兼容的浏览器。这看起来不那么丑陋,但仍然不是最佳解决方案。)
Context
Part of the administrator side of our application requires the user to edit various types of content, which involves using a rich text editor or using files to generate content that can be seen by the 'client side' users of the application. It's kind of a domain-specific CMS lite.
Because this 'content' can be used in various parts of the application, it is included as a seperate relation in some of our domain entities. We decided to make our own tag library that defines some form fields that can be used to edit this content when an administrator edits an entity that includes a piece of content.
Question
What we'd like to be able to do is the following.
<form:form modelAttribute=...>
<olo:content-editor path="content"/>
<!-- Other form fields for this entity -->
<form:.../>
<form:.../>
</form:form>
The olo:content-editor
tag then generates a number of form fields based on what type of content is needed. This means it may (or, depending on the type of content, may not!) generate the filebased-content
tag which contains:
<input type="file" name="file/>
Which can be used to replace the file associated with the file based content.
The problem is that the Spring docs indicate that the file upload requires the form to have the enctype
to define that it's sending multipart form data. As the file upload is part of the tag and not the form itself, we find this is undesirable. We would like to be able to use our olo:content-editor
tag in forms without having to change the form enctype
attribute. Is this possible?
Possible solutions
We can think of two client-side hacks that may resolve our problem, but both seem to be rather ugly solutions:
- Include a script in filebased content tag that changes the form
enctype
when it's loaded, so that it is always set to the appropriate type. (Very ugly.) - Submit the file data as a regular hidden form field, of which the data is set by using the HTML5 File API (administrators use a compliant browser. This seems far less ugly but still not an optimal solution.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论