Grails导出插件不要下载任何文件

Grails导出插件不要下载任何文件

本文介绍了Grails导出插件不要下载任何文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用grails导出插件将我的域类导出到xls和csv文件。

I want to use grails export plugin to get my Domain classes exportables into xls and csv files.

在我的主要布局中,名为 front.gsp ,我这样做:

In my main layout named front.gsp, I did that :

… …

进入我的 DomainClassController.groovy 我这样做:

def exportTest() { if(!params.max) params.max = 10 if(params?.format && params.format != “html”){ response.contentType = grailsApplication.config.grails.mime.types[params.format] response.setHeader(“Content-disposition”, “attachment; filename=contacts.${params.extension}”) exportService.export( params.format, response.outputStream, ContactDTO.list(params), [:], [:]) [contactDTOInstanceList: ContactDTO.list( params )] } }

我还创建了一个名为 exportTest.gsp 的视图到我的控制器的视图文件夹中,只是空文件暂时解决404问题。

I also create a view named exportTest.gsp into my view folder of my controller, just empty file to temporarely solve 404 issue.

我没有任何错误但是当我点击任何导出按钮时,我正在重定向到我的页面 exportTest.gsp 和 没有文件被下载

I did not have any error messages but when I’m clicking on any export button, I’m redirecting to my page exportTest.gsp and no file is downloaded

如何使用grails导出插件获取下载的文件?我按照用户指南,没有任何错误(已解决),但没有创建任何文件?

How can I get a downloaded file using grails export plugin ? I follow the user guide, don’t have any errors (solved) but no file is created ?

感谢您阅读!

Snite

编辑:我只是看到,点击导出链接时,生成的网址是:

EDIT : I just see that, when clicking on export link, the generated url is :

http://localhost:8080/site/controller/action?format=excel&extension=xls

但是奇怪的是,在我的控制器中输入println参数时,输出为:

But strangely, when making a “println params” into my controller, output is :

[extension:xls, action:exportTest, format:null, controller:contactDTO, max:10]

params.format为null BUT设置为url?!!!

params.format is null BUT set into url ?!!!

更改URL(并调整控制器代码):

Change url like that (and adapting controller code) :

http://localhost:8080/site/controller/action?formatD=excel&extension=xls

给我以下错误:

Firefox ne peut trouver le fichier à l’adresse http://localhost:8080/site/controller/action?formatd=excel&extension=xls.

推荐答案

我的猜测是params.format正在混淆Grails内容协商格式参数。导出标签似乎生成了这种格式,除非修改后,您可以创建一个链接到您的 exportTest ,并传递所需的格式,就可以使用exportFormat变量名。在控制器上使用exportFormat而不是格式。

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.

还要确保 ContactDTO.list(params)正在返回一些值。另一点是确保你已经在你的配置中定义了内容类型,就像在导出插件中所说的一样。文档

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:

CSV

在您的控制器中:

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 )] } }

这篇关于Grails导出插件不要下载任何文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!


比丘资源网 » Grails导出插件不要下载任何文件

发表回复

提供最优质的资源集合

立即查看 了解详情