SolveItproblems

SolveITProblems is a project of QFormat.net intellectual club

SolveITProblems это проект интеллектуального клуба QFormat.net

вторник, 7 февраля 2012 г.

Grails File Upload/Download Controller

Hi,
are you ready to upload and download file in Grails?
So, here the Controller side:

Uploading

        //File Saving
        def attachFile = request.getFile("attach")
        if (attachFile)
        {
            def file = new File(System.currentTimeMillis()+attachFile.originalFilename)
            attachFile.transferTo(file)
        }      
        //END File Saving


Downloading

    def download = {

            def file = new File(params.filePath)    
            response.setContentType("application/octet-stream")
            response.setHeader("Content-disposition", "attachment;filename=${params.filePath}")
            response.outputStream << file.newInputStream()                
        
    }

View side:
In uploading you need just HTML form with input type="file" and your form tag should contain the following attributes:
 method="POST" enctype="multipart/form-data"
in a case of download, it's just a request.

Комментариев нет:

Отправить комментарий