form表单导出文件流

1.使用form导出

function postExcelFile(params, url) { //params是post请求需要的参数,url是请求url地址
    var form = document.createElement("form");
    form.style.display = 'none';
    form.action = url;
    form.method = "post";
    document.body.appendChild(form);

    for(var key in params){
      var input = document.createElement("input");
      input.type = "hidden";
      input.name = key;
      input.value = params[key];
      form.appendChild(input);
    }

    form.submit();
    form.remove();
  }
  //点击导出按钮导出excel表格
  exportButton.onclick = function() {
    var params = {};
    postExcelFile(params, "http://www.XXX_excel");
  }

问题:如何设置导出名字?????

2.使用axios导出,乱码无法解决

axios({
		method:'get',
		url:`${data.url}${data.params?'?'+data.params : ''}`,
		responseType:'blob'
	}).then(res=>{
		const link = document.createElement('a')
		let blobtype = ''
		if(data.type == 'dxf'){
			blobtype = 'image/vnd.dxf'
		}else if(data.type=='png'){
			blobtype = 'image/png'
		}
		let blob = new Blob([res.data],{
			type:blobtype
		})
		link.style.display = 'none',
		link.href = URL.createObjectURL(blob)
		link.download = data.fileName;
		document.body.appendChild(link)
		link.click()
		document.body.removeChild(link)
		this.$message.success('导出成功')
	}).catch(err=>{
		this.$message.error('导出失败')
	})
发布了18 篇原创文章 · 获赞 2 · 访问量 3926

猜你喜欢

转载自blog.csdn.net/qq_42928070/article/details/103454835
今日推荐