Bad*_*ral 5 grails text download
我来自asp.net的剃须刀.通常我会使用[FilePost],但不知道如何在grails中使用它.情况就是这样
我有一个控制器
class MyController{
def index{ }
}
Run Code Online (Sandbox Code Playgroud)
然后我以索引的形式链接索引
<g:link controller="MyController" action="downloadFile">Download</g:link><br>
Run Code Online (Sandbox Code Playgroud)
我想要做的是取一个字符串(无论它是什么),我希望它提示用户下载包含该字符串的文本文件.谢谢!
工作解决方案:
def downloadFile =
{
File file = File.createTempFile("temp",".txt")
file.write("hello world!")
response.setHeader "Content-disposition", "attachment; filename=${file.name}.txt"
response.contentType = 'text-plain'
response.outputStream << file.text
response.outputStream.flush()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1746 次 |
| 最近记录: |