Grails export plugin don't download any file

孤街浪徒 提交于 2019-12-01 19:12:16

My guess is the params.format is getting confused with Grails content negotiation format in parameter. The export tag seems to generate that format, unless that gets fixed you can create a link to your exportTest and pass the format you want under lets say exportFormat variable name. On your controller use exportFormat instead of format.

Also make sure that ContactDTO.list(params) is returning some values. Another point is make sure you have defined the content types in you config as it says in the export plugin documentation

So you might need to create a link like this:

<a class="csv" href="/testData/loader/exportTest?exportFormat=csv&extension=csv"> 
  CSV 
</a>

In your controller :

def exportTest() {
        if(!params.max) params.max = 10

        if(params?.exportFormat && params.exportFormat != "html"){
            response.contentType = grailsApplication.config.grails.mime.types[params.exportFormat] 
            response.setHeader("Content-disposition", "attachment; filename=contacts.${params.extension}")

            exportService.export(
                params.exportFormat,
                response.outputStream,
                ContactDTO.list(),
                [:],
                [:])
            [ContactDTO: ContactDTO.list( params )]
        }
    }
sdkao

I did some by passing through this way

params.format=params.extension<br/>
        if(params.format=='xls') {
            params.format='excel'
        }

Explanation: As the params.format is null, I did a system print for extension, And I remark that the extension is the same as format for all extension except xls. So if xls I the format must be 'excel' for excel exporting. This is why. In my cas it works fine for csv, excel, pdf and xml. Not tested with ODD etc..

I am using export plugin version 2.0.0 with Grails 3. As per the internal implementation in Plugin's ExportTagLib.groovy the argument format has been renamed to avoid conflict with grails 3

ExportTagLib.groovy

Map params = [f: format, extension: extension] + parameters

Using params.f will work fine for now. Sample code.

        if (params.f && params.f != "html") {
        response.contentType = Holders.config.grails.mime.types[params.f]
        response.setHeader("Content-disposition", "attachment; filename=${message(code: 'dpInvoice.exportBillFacilitator.filename', default: 'exportBillFacilitator')}.${params.extension}")
        exportService.export(params.f, response.outputStream, results, fields, labels, formatters, parameters)
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!