a标签请求
html代码:
<a href="地址" ></a>
php代码:
$filename = rawurlencode($filename);
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
header("Content-Length: " . strlen($text));
echo $text;
ajax请求
php页面就不写了就是给ajax传数据
<button type="button" class="layui-btn layui-btn-sm layui-btn-primary " onclick="exportTXT()" >导出TXT文件</button>
function exportTXT() {
var tid = '{$tid}';
$.get('url',参数,function (res) {
if (res.code == 0){
var text = '';
$.each(res.data,function (index,value) {
text += value + '\n';
})
console.log(text);
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', 'HS'+tid+'.txt');
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}else{
layer.msg(res.msg,{icon:5});
}
});
}
|