如何强制grails下载csv文件?

发布于 2024-08-22 09:50:45 字数 1208 浏览 10 评论 0原文

在我的 gsp 视图中,我有以下代码:

<g:each in="${fileResourceInstanceList}" status="i" var="fileResourceInstance">

<tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
<td>${fileResourceInstance.decodeURL()}</td>

<td><a href="${createLinkTo( dir:"/upload_data/datasets/ds"+signalDataInstance.datasetID , file: fileResourceInstance.decodeURL(), absolute:true )}" target="_new">view</a></td>

<td><g:link action="deleteFile" id="${fileResourceInstance.replace('.','###')}" params="[rs:signalDataInstance.datasetID]" onclick="return confirm('Are you sure?');"> delete </g:link></td>
</tr>
</g:each>

我想下载我的 csv 文件,而不是在浏览器中读取我的 csv 文件!
如何强制下载?

这是我的控制器中的代码部分:

    def f = new File( "${linkDir}".toString() )
    if( f.exists() ){
        f.eachFile(){ file->
        if( !file.isDirectory() )
            fileResourceInstanceList.add( file.name )
        }
    }

在我的代码中哪里添加此部分以强制下载? :

response.setHeader("Content-disposition", "attachment; filename=" + file.name + ".csv");
render(contentType: "text/csv", text: file.name.toString());

In my gsp view, I have this code :

<g:each in="${fileResourceInstanceList}" status="i" var="fileResourceInstance">

<tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
<td>${fileResourceInstance.decodeURL()}</td>

<td><a href="${createLinkTo( dir:"/upload_data/datasets/ds"+signalDataInstance.datasetID , file: fileResourceInstance.decodeURL(), absolute:true )}" target="_new">view</a></td>

<td><g:link action="deleteFile" id="${fileResourceInstance.replace('.','###')}" params="[rs:signalDataInstance.datasetID]" onclick="return confirm('Are you sure?');"> delete </g:link></td>
</tr>
</g:each>

I would like to download my csv files, and not read my csv files in my browser !
How to force download ?

Here code part in my controller :

    def f = new File( "${linkDir}".toString() )
    if( f.exists() ){
        f.eachFile(){ file->
        if( !file.isDirectory() )
            fileResourceInstanceList.add( file.name )
        }
    }

Where to add this part in my code to force download ? :

response.setHeader("Content-disposition", "attachment; filename=" + file.name + ".csv");
render(contentType: "text/csv", text: file.name.toString());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

轻许诺言 2024-08-29 09:50:45

对 render 的调用是问题所在 - 直接写入响应输出流:

response.setHeader "Content-disposition", "attachment; filename=${file.name}.csv"
response.contentType = 'text/csv'
response.outputStream << file.text
response.outputStream.flush()

The call to render is the problem - write directly to the response output stream:

response.setHeader "Content-disposition", "attachment; filename=${file.name}.csv"
response.contentType = 'text/csv'
response.outputStream << file.text
response.outputStream.flush()
昨迟人 2024-08-29 09:50:45

您还可以使用 导出插件 - 它可以满足您的需求,并且有一些不错的附加代码它将遵循关联等以及其他输出格式。

根据您的要求,可能是一个更易于维护的解决方案。

You can also use the export plugin - it does what you want, and has some nice additional code that will follow associations, etc, as well as other output formats.

Might be a more maintainable solution depending on your requirements.

桃扇骨 2024-08-29 09:50:45

这是修复:

在视图(gsp)中:

<td><g:link action="download" id="${fileResourceInstance}" params="[rs:signalDataInstance.datasetID]" > download </g:link></td>

在控制器中:

def download = {

def filename = params.id

def dsId = params.rs

def webRootDir = servletContext.getRealPath("/")

def linkDir = "${webRootDir}/upload_data/datasets/ds${dsId}"

    def file = new File( "${linkDir}".toString() + File.separatorChar + filename + ".csv" ) 

response.setHeader "Content-disposition", "attachment; filename=${file.name}"
      response.contentType = 'text/csv'
      response.outputStream << file.text
      response.outputStream.flush()

}

Here is the fix :

In view (gsp) :

<td><g:link action="download" id="${fileResourceInstance}" params="[rs:signalDataInstance.datasetID]" > download </g:link></td>

In controller :

def download = {

def filename = params.id

def dsId = params.rs

def webRootDir = servletContext.getRealPath("/")

def linkDir = "${webRootDir}/upload_data/datasets/ds${dsId}"

    def file = new File( "${linkDir}".toString() + File.separatorChar + filename + ".csv" ) 

response.setHeader "Content-disposition", "attachment; filename=${file.name}"
      response.contentType = 'text/csv'
      response.outputStream << file.text
      response.outputStream.flush()

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文