我希望即时创建文件,并在GRAILS应用程序中向用户提供下载链接。
我遵循了here的方法。我没有错误,但是似乎没有用。这是我的 Controller 代码。
渲染(文件:pptFile,文件名:'someppt.pptx',contentType:'application / octet-stream')
客户端代码进行AJAX调用以从服务器检索文件。它不会导致服务器强制在客户端(浏览器)上下载文件。这是客户端代码。
$.ajax({
type : 'POST',
url : '<<URL>>',
success: function(result) {
var uri = 'data:application/octet-stream;charset=UTF-8,' +
encodeURIComponent(result);
window.open(uri, 'somePPT.pptx');
},
failure: function(){
alert ('failure')
}
});
最佳答案
也许与此类似(表述为,但用于下载json文件):
def someControllerMethod() {
def dlContent = someService.marshalJson()
def contentType = "application/octet-stream"
def filename = "someFilename.json"
response.setHeader("Content-Disposition", "attachment;filename=${filename}")
render(contentType: contentType, text: dlContent as JSON)
}
关于grails - Grails应用程序中的文件下载功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38447685/