Загрузите файл .exe с ошибкой в ​​Angular интерфейсе - PullRequest
0 голосов
/ 28 мая 2020

Я использую это в node JS: -

let file = `${process.cwd()}/server/downloads/JRuler.exe`;
   let filename = path.basename(file);
   let stat = fs.statSync(file);

   res.writeHead(200, {
       'Content-Type': 'application/octet-stream',
       'Content-Length': stat.size,
       'Content-disposition': 'attachment; filename=' + filename
   });

   let readStream = fs.createReadStream(file);
   readStream.pipe(res);

И это в Angular

  download() {
    this.clientAPI.get(Endpoints.DOWNLOAD).toPromise()
    .then(res => {
      console.log('res')
      let blob = new Blob([new Uint8Array(res.file)],{type:'application/octet-stream'});
      console.log('blob',blob);
      let a = document.createElement('a');
      var ua=navigator.userAgent;
      var msie= ua.indexOf("MSIE") > -1 || ua.indexOf("Trident/") > -1;
      if(msie){
        navigator.msSaveOrOpenBlob(blob,res.filename);
      }
      else{
      a.href = (URL.createObjectURL(blob));
      a.download = res.filename;
      document.body.appendChild(a);
      a.click();
      a.remove();
      }
    })
    .catch(err => console.error("download error = ", err))
  }

Но я получаю сообщение об ошибке: - enter image description here

Подскажите, пожалуйста, как я могу работать с angular интерфейсом.

1 Ответ

0 голосов
/ 28 мая 2020

responseType blob необходим для http-клиента, чтобы знать, как анализировать ответ и возвращать подписчику или обещанию в вашем случае.

 download() {
        this.clientAPI.get(Endpoints.DOWNLOAD, {responseType: 'blob'}).toPromise()
        .then(res => {
          console.log('res')
          let blob = new Blob([new Uint8Array(res.file)],{type:'application/octet-stream'});
          console.log('blob',blob);
          let a = document.createElement('a');
          var ua=navigator.userAgent;
          var msie= ua.indexOf("MSIE") > -1 || ua.indexOf("Trident/") > -1;
          if(msie){
            navigator.msSaveOrOpenBlob(blob,res.filename);
          }
          else{
          a.href = (URL.createObjectURL(blob));
          a.download = res.filename;
          document.body.appendChild(a);
          a.click();
          a.remove();
          }
        })
        .catch(err => console.error("download error = ", err))
      }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...