以下操作旨在写入 bytes
的二进制内容直接到客户端完全绕过 Grails View 层:
def actionName = {
byte[] bytes = ...
ServletOutputStream out = response.getOutputStream()
out.write(bytes)
out.flush()
out.close()
return false
}
我的印象是
return false
会使 Grails 完全跳过 View 层。然而,情况似乎并非如此,因为上述代码仍然使 Grails 搜索 /WEB-INF/grails-app/views/controllerName/actionName.jsp
(由于 404 失败,因为不存在这样的文件)。题:
最佳答案
您应该返回 null 或什么都不返回,这被解释为 null。以下是发送动态生成的 PDF 的操作中的一些工作代码:
def pdf = {
byte[] content = ...
String filename = ...
response.contentType = 'application/octet-stream'
response.setHeader 'Content-disposition', "attachment; filename=\"$filename\""
response.outputStream << content
response.outputStream.flush()
}
关于grails - 绕过 Grails View 层将二进制内容直接写入客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1655872/