Grails file upload - how to recognize file andor content type

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

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.

Answers

http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/web/multipart/commons/CommonsMultipartFile.html http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/web/multipart/commons/CommonsMultipartFile.html

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.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/5019478/grails-file-upload-how-to-recognize-file-and-or-content-type

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils