简体   繁体   English

从服务器下载文件时获取`tar: Error opening archive: Unrecognized archive format`

[英]Getting `tar: Error opening archive: Unrecognized archive format` while downloading a file from server

I have a sample.tar.gz report that's getting generated on my remote system and needs to be downloaded on a button click.我有一个在我的远程系统上生成的sample.tar.gz报告,需要单击按钮下载。 I am sending a request via the server and needs to be added to file sample.tar.gz on local system.我正在通过服务器发送请求,需要将其添加到本地系统上的文件sample.tar.gz

On the server side code, I do see binary data in the response在服务器端代码中,我确实在响应中看到了二进制数据

body:"�q'l_�=ks�6���_��^��*q���d��i��bIs9���-H��ṙl����5�_�P4���e�@h��F���}�y���,I�L]/~Bj�,~�USU%E�\r�I�UM�F��R��4C� |��q�\tn[�4���_�^z"�k�u���*�/������G�,��iC믚���%�8Y\t��‚\r���E!YP���U�r/@�d0<[��W=ũ��5t���9�&�X��`cF�5��AJ%[T�I�h�D������L6���[\nC���-5�Fy�9��L��IWy@9hv\+�E%�@N���r�٦��s�5"�P*ğa�����h\̆(��&!Þ!x9a�~�"5���('��>�Iwڂ�g��"�..."

But when the below code runs, I am able to download the file:但是当下面的代码运行时,我可以下载文件:

export const SystemReport = () => {
  downloadReport = (url) => {
    const options = {
      credentials: 'same-origin',
      responseType: 'arraybuffer',
      method: 'GET',
      headers: {
        'Accept': 'application/gzip; charset=utf-8',
        'Content-Type': 'application/json; charset=utf-8'
      }
    }
    fetch(url, options)
      .then(res => {return res.blob()})
      .then(blob => {
        let url = window.URL.createObjectURL(blob, {type: 'application/zip'})
        let a = document.createElement('a')
        a.href = url
        a.download = 'test.tar.gz'
        a.click()
      }).catch(err => console.error(err))
  }
  const fileUrl = `<System-Path>/api/system-report/download/${<file-name>}`
  return (
    <div className='download-report'>
      <a href='#' onClick={() => downloadReport(fileUrl)} target='_blank' download>
        <Download16 />
      </a>
  </div>
  )
}

But when I try to unzip the file, I get the below error但是当我尝试解压缩文件时,出现以下错误

# tar -xvzf ~/Downloads/sample.tar.gz 
tar: Error opening archive: Unrecognized archive format

Tried looking for some solutions online since I am still learning about these things, but no luck yet.尝试在网上寻找一些解决方案,因为我仍在学习这些东西,但还没有运气。 Any help will be appreciated!任何帮助将不胜感激!

Edit : There is a return, but I somehow forgot to add it here.编辑:有一个回报,但我不知何故忘记在此处添加它。 Have edited the code.已编辑代码。

You have made a mistake in the following line:您在以下行中犯了错误:

      .then(res => {res.blob()})

The function res => {res.blob()} is equivalent (if we don't take the differences between arrow and regular functions into account) to:函数res => {res.blob()}等效于(如果我们不考虑箭头函数和常规函数之间的差异):

function (res) {
  res.blob();
}

It has no return value, so blob in the next .then block will be equal to undefined .它没有返回值,因此下一个.then块中的blob将等于undefined

You need to use the res => res.blob() syntax which is equivalent to the following code:您需要使用res => res.blob()语法,它等效于以下代码:

function (res) {
  return res.blob();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM