情景描述:
有一个需求,客户上传文件,向服务里导入数据。客户需要使用我们特定的文件模板上传。模板需要再我们的系统上下载,在使用。
功能描述:
后端服务给我们文件流,我们使用blob对象文件下载到本机。
base64ToArrayBuffer ( base64 ) {
let time = this . getDateTime ( new Date ( ) )
let name = '文档模板' ;
var binaryString = window. atob ( base64)
var len = binaryString. length
var bytes = new Uint8Array ( len)
for ( var i = 0 ; i < len; i++ ) {
bytes[ i] = binaryString. charCodeAt ( i)
}
var _Blob = new Blob ( [ bytes] , { type: "application/octet-binary" } ) ;
let link = document. createElement ( "a" ) ;
let href = window. URL . createObjectURL ( _Blob) ;
link. style. display = "none" ;
link. href = href;
link. download = name + time + '.xlsx' ;
document. body. appendChild ( link) ;
link. click ( ) ;
document. body. removeChild ( link) ;
window. URL . revokeObjectURL ( href) ;
} ,
getDateTime ( time ) {
let hh = time. getHours ( ) ;
let mf = time. getMinutes ( ) < 10 ? '0' + time. getMinutes ( ) : time. getMinutes ( ) ;
let ss = time. getSeconds ( ) < 10 ? '0' + time. getSeconds ( ) : time. getSeconds ( ) ;
let yy = time. getFullYear ( ) ;
let mm = time. getMonth ( ) + 1 ;
let dd = time. getDate ( ) ;
return yy + '' + mm + '' + dd + '' + hh + '' + mf + '' + ss;
} ,
downFile ( info ) {
this . DownLoadFile ( info)
} ,
DownLoadFile ( info ) {
Api. downLoadFile ( info) . then ( data => {
if ( data. status) {
this . base64ToArrayBuffer ( data. value)
} else {
this . $Notice. error ( { title: '下载失败' } ) ;
}
} )
}