ajax生成csv并下载

  • Post author:
  • Post category:其他


$('#export').click(function () {
    var date_range = $('#date-range').val();
    var full_kw = $('#full_kw').val();
    $.ajax({
        type: 'post',
        url: "",
        data: {
            date_range: date_range,
            full_kw: full_kw
        },
        contentType: "application/json",
        success: function (result) {
        	// result格式,不能是json
        	// result = 'a,b,c\n5,6,7\n1,2,3',
            console.log(result);
            // "\ufeff"防止乱码
            result="\ufeff"+result;
            var blob = new Blob([result], {type: 'application/vnd.ms-excel'});
            var downloadUrl = URL.createObjectURL(blob);
            var a = document.createElement("a");
            a.href = downloadUrl;
            a.download = "data.csv";
            document.body.appendChild(a);
            a.click();
        },
        error: function (error) {
            console.log(error);
        }
    });
});



版权声明:本文为weixin_42185136原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。