其他分享
首页 > 其他分享> > JS客户端下载MinIO保存的文件

JS客户端下载MinIO保存的文件

作者:互联网

js 在客户端下载MinIO的文件

特别要注意一点:

accessKey,secretKey长度小于20时,在js中无法访问MinIO服务器
    /* 下载文档 */
    async downLoad(row){
      let data={
        id:row.id,
        requestType:2 //
      }
      let boolean=await(this.judge(data)); 
      if(boolean){
         dir.look({ id: row.id }).then(res =>{
          let resData = res.data.data;
          let docUrl="";
            if (resData !== null && resData.docUrl !== null) {
              console.log('docUrl',resData.docUrl)
              docUrl = resData.docUrl;
            }else if(resData !== null && resData.pdfUrl !== null){
              console.log('pdfUrl',resData.pdfUrl)
               docUrl = resData.pdfUrl;
            }else{
              this.$message.info("该文档暂时不可下载!")
              return;
            }

            console.log("minio");
            var minioClient = new Minio.Client({
                endPoint: 'localhost',
                port: 9000,
                useSSL: false,
                accessKey: 'XXXXXX',
                secretKey: 'XXXXXX'
            });  
            
            // let filename=row.docName;
            // let filepath=docUrl.substring(docUrl.indexOf('upload/'));
            // let fileExtension=row.suffix;
            let filename="APS排产器产品原型常爱平";
            let filepath="1212121.pdf"
            let fileExtension="pdf";
            let list=new Array();
            var size = 0;
            minioClient.getObject('bladex', filepath, function(err,dataStream) {
              if (err) {
                return console.log(err)
              }
              dataStream.on('data', function(chunk) {
                size += chunk.length
                list.push(chunk);
              })
              dataStream.on('end', function() {
                console.log('End. Total size = ' + size);                
                let u8arr = new Uint8Array(size)
                let u8arrindex=0;
                for(let i=0;i<list.length;i++){
                  for(let j=0;j<list[i].length;j++){
                    u8arr[u8arrindex]=list[i][j];
                    u8arrindex=u8arrindex+1;
                  }
                }

                let filetype="";
                switch(fileExtension)
                {
                  case 'pdf':
                    filetype="application/pdf;charset=utf-8";
                    break;
                  case 'txt':
                    filetype="text/plain;charset=utf-8";
                    break;
                  case 'xls':
                    filetype="application/x-xlw;charset=utf-8";
                    break;
                  case 'doc':
                    filetype="application/msword;charset=utf-8";
                    break;
                  case 'jpg':
                    filetype="image/jpeg;charset=utf-8";
                    break;
                  case 'png':
                    filetype="image/png;charset=utf-8";
                    break;
                  case 'ppt':
                    filetype="application/x-ppt;charset=utf-8";
                    break;
                }
                var file = new File([u8arr], "", { type: filetype });                
                FileSaver.saveAs(file,filename+"."+fileExtension);
              })
              dataStream.on('error', function(err) {
                console.log(err)
              })
            });
         })
      }else{
        this.ifApply=true;
        this.applyType="下载";
      }
    },

  

标签:size,console,MinIO,resData,JS,docUrl,let,row,客户端
来源: https://www.cnblogs.com/mikechang/p/14778091.html