Grails file upload - how to recognize file and/or content type?

后端 未结 3 1786
执笔经年
执笔经年 2021-02-08 16:05

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 c

3条回答
  •  难免孤独
    2021-02-08 16:55

    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:

    
    

    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.

提交回复
热议问题