流文件上传和下载

1、文件上传下载格式化方法

上传

const $this = this;
const fileData = document.getElementById('file').files[0];
const params = new FormData();
const NameNode = `${this.pollutionText()}导入模板`;
const [filesName1] = this.filesName.split('.');
console.log(NameNode, this.filesName.split('.')[0]);
params.append('categoryId', this.pollutionTypeId);
params.append('file', fileData);
dataImport(params)
	.then(res => {
	console.log(res);
	$this.filesName = '';
	$this.BatchImportFlag = false;
	$this.reset();
	const obj = document.getElementById('file');
// 清空input框选中文件值
	obj.value = '';
})
	.catch(err => {
	console.log(err);
});

下载 ---Remark请求接口响应类型(必写): responseType: 'blob';

const blob = res;
// FileReader主要用于将文件内容读入内存
const reader = new FileReader();
reader.readAsDataURL(blob);
// onl oad当读取操作成功完成时调用
reader.onload = e => {
	const a = document.createElement('a');
// 获取文件名fileName
// let fileName = res.headers['content-disposition'].split('=');
// fileName = fileName[fileName.length - 1];
// fileName = fileName.replace(/"/g, '');
	a.download = 'fileName.pdf';
	a.href = e.target.result;
	document.body.appendChild(a);
	a.click();
	document.body.removeChild(a);
};
// const url = URL.createObjectURL(new Blob([res]));
// const link = document.createElement('a');
// debugger;
// const fileName = `${new Date().getTime()}.xlsx`;
// link.style.display = 'none';
// link.href = url;
// link.setAttribute('download', fileName);
// document.body.appendChild(link);
// link.cilck();
// document.body.removeChild(link);

2、文件上传前端格式限制

	<input
		ref="showinput2"
		v-show="false"
		type="file"
		@change="upFile()"
		accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
		name=""
		id="file"/>
- accept属性限制文件 PDF DOC DOCX 
- accept: application/pdf,.doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document
上一篇:重定向后Cookie丢失问题解决办法


下一篇:洛可可工作室第二次培训