Grails文件上传 - 如何识别文件和/或内容类型? [英] Grails file upload - how to recognize file and/or content type?
问题描述
request.getFile()
我可以很容易地获得字节流。但在此之前,我想检查以下内容:$ b $ ul
这怎么能做到?文件上传到服务器之前甚至有可能吗?我想阻止上传大文件。 所有的信息都包含在
CommonsMultipartFile
的对象,你可以把你的请求参数。 你可以像这样使用它(在你的控制器中)
def uploaded = {
def CommonsMultipartFile uploadedFile = params.fileInputName
def contentType = uploadedFile.contentType
def fileName = uploadedFile.originalFilename
def size = uploadedFile.size
}
至于阻止大文件上传,这可以通过将以下内容添加到您的表单:
< INPUT name =fileInputNametype =file maxlength =100000>
但并非所有的浏览器都支持它。另一个限制是你的容器上传限制(见Tomcat配置或者你正在使用的任何容器)。
除此之外,你必须检查大小并拒绝它控制器。
I'm a Grails beginner, so please be patient with me. Currently I'm having hard times manipulating file uploads. As far as I understand using request.getFile()
I can easily get the stream of bytes. But before I do that, I want to check the following:
- file name of the file being uploaded
- file size of the file being uploaded
- content/file type of the file being uploaded
How can this be done? Is it even possible before the file is uploaded to the server? I would like to block uploading of large files.
All the information is contained in the CommonsMultipartFile
object that you can cast your request parameter to.
You can use it like that (in your controller)
def uploaded = {
def CommonsMultipartFile uploadedFile = params.fileInputName
def contentType = uploadedFile.contentType
def fileName = uploadedFile.originalFilename
def size = uploadedFile.size
}
As far as blocking large file uploads, this could be done by adding the following to your form:
<INPUT name="fileInputName" type="file" maxlength="100000">
but not all browsers will support it. The other limit is you container upload limit (see Tomcat configuration or whatever container you are using).
Other than that, you have to check the size and reject it in the controller.
这篇关于Grails文件上传 - 如何识别文件和/或内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!