问题描述
我的 GSP 中有这个 ajax 调用:
I have this ajax call in my GSP:
$.ajax({
url: '${request.contextPath + '/Ticket/passAll'}',
type: 'POST',
data: data,
success: function() {
alert("Success");
}
});
这是来自我的控制器操作的代码块:
This is code block from my controller action:
response.setHeader("Content-disposition", "attachment; filename=sample.csv")
response.contentType = "application/vnd.ms-excel"
def outs = response.outputStream
def cols = [:]
tickets.each() {
outs << it.ticketNo + ";" + it.subject
outs << "
"
}
outs.flush()
outs.close()
我从通过 $.Ajax 方法从视图传递的数据中获取票证列表.比我将该数据格式化为 CSV 并且我想将该数据导出为 CSV 文件但没有任何反应.数据被发送到客户端,但没有文件可供下载,因为内容配置格式不正确.我错过了什么?我试过做这样的事情:
I get tickets list from data that I pass from view via $.Ajax method. Than I format that data as CSV and than I want to export that data as CSV file but nothing happens. The data is send to client but there is no file for download as content-disposition is not well formed. What am I missing?I've tried to do something like:
$.ajax({
url: '${request.contextPath + '/Ticket/passAll'}',
type: 'POST',
data: aoData,
dataType: 'text',
success: function(result) {
var uri = 'data:application/csv;charset=UTF-8,' + encodeURIComponent(result);
window.open(uri, 'tiketi.csv');
}
});
并且在控制器中我生成纯字符串,但是这样我得到一个没有扩展名的文件,这是不可接受的.
and in controller I generate plain string, but that way I get a file without extension which is unacceptable.
我怎样才能做到这一点?谢谢.
How can I achieve this? Thank you.
推荐答案
你不需要通过 ajax 来做.该页面不会重定向文件下载.
You dont need to do it via ajax. The page will not redirect for file downloads.
这篇关于从 Ajax 下载文件(有点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!