以excel格式导出时,如何在ajax中指定文件名
How to specify the filename in ajax while exporting in excel format
在这段代码中,我想在下载excel格式时指定文件名。
if(comp_id != "Select Company")
{
$.ajax({
url: 'includes/export.php',
data: {
action: 'compreport',
'comp':comp_id,
},
type: 'post',
success: function (output) {
$("#ereportview").html(output);
window.open('data:application/vnd.ms-excel,' + encodeURIComponent( $('div[id$=ereportview]').html()));
e.preventDefault();
},
async: false
});
}
在success
回调中使用此下载功能
function download(filename, text, mime) {
var element = document.createElement('a');
element.setAttribute('href', 'data:'+mime+',' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
你可以这样使用它:
if(comp_id != "Select Company") {
$.ajax({
url: 'includes/export.php',
data: {
action: 'compreport',
'comp':comp_id,
},
type: 'post',
success: function (output) {
download("yourfile.xlsx", output, 'application/vnd.ms-excel');
},
async: false
});
}
您可以为正在下载的内容类型指定MIME。您可以从SitePoint 获得
相关文章:
- 无法在通过jQuery的ajax加载的页面中执行javascript
- 如何通过ajax刷新JSF填充的javascript变量
- 如何在php文件中获取$.post-ajax传递的值
- Replacing $ .ajax?
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- Ajax发布表单序列化,发布引号'
- 通过Ajax将JavaScript函数传递给PHP文件
- ajax请求的顺序总是不同的
- 可以't使用Polymer's的核心ajax
- Ajax Live搜索发布到Laravel视图
- Ajax聊天消息重复而不仅仅是更新
- JS-使用Ajax获取文件名
- 类似的文件名ajax
- 通过AJAX上传图片-如何将文件名传递到服务器
- 以excel格式导出时,如何在ajax中指定文件名
- 如何在ajax调用中给出动态json文件名作为url
- 使用AJAX将文件名插入数据库
- 文件上载与Ajax -没有得到完整的文件名
- 获取通过 AJAX 上传的文件的文件名
- 调用文件名的if语句的AJAX请求