return $r; } /** * @param int $page 页数 * @param int $pagesize 每页显示数量 * @return mixed */ function link_find($page = 1, $pagesize = 100) { $arr = link__find($cond = array(), array('rank' => -1), $page, $pagesize); return $arr; } /** * @param $id * @return bool 返回FALSE失败 TRUE成功 */ function link_delete($id) { if (empty($id)) return FALSE; $r = link__delete(array('id' => $id)); link_delete_cache(); return $r; } //--------------------------kv + cache-------------------------- /** * @return mixed 返回全部友情链接 */ function link_get($page = 1, $pagesize = 100) { $g_link = website_get('friends_link'); if (empty($g_link)) { $g_link = link_find($page, $pagesize); $g_link AND website_set('friends_link', $g_link); } return $g_link; } // delete kv and cache function link_delete_cache() { website_set('friends_link', ''); return TRUE; } ?>javascript - my files (pdf, xlsx, docx ..) are downloaded corrupt or with errors, in Reactjs - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - my files (pdf, xlsx, docx ..) are downloaded corrupt or with errors, in Reactjs - Stack Overflow

programmeradmin1浏览0评论

I am working with React Js and Axios to make requests to an API. I am trying to download files via API with ReactJs, but when they download and try to open, I get an error message: 'the file is damaged or damaged'.

The files are obtained through a GET request and can be of type pdf, xlxs, docx and others), and the mime is obtained through props that es from a parent ponent

This is my code (a part of my ponent)

fetchFile(){

  axios
    .get(`/someurl/thefiles/${this.props.file.id}`, { headers })

    .then(response => {
    
          let url = window.URL.createObjectURL(new Blob([response.data]));
          const link = document.createElement("a");
          link.href = url;
          link.setAttribute("download",
          `${this.props.file.name}.${this.props.file.mime}`);
          document.body.appendChild(link);
          link.click();
  });


}

render(){

  return(
      
      <button onClick={this.fetchFile}> Download file </button>
    
  )

}
<script src=".6.1/umd/react.production.min.js"></script>
<script src=".6.1/umd/react-dom.production.min.js"></script>

I am working with React Js and Axios to make requests to an API. I am trying to download files via API with ReactJs, but when they download and try to open, I get an error message: 'the file is damaged or damaged'.

The files are obtained through a GET request and can be of type pdf, xlxs, docx and others), and the mime is obtained through props that es from a parent ponent

This is my code (a part of my ponent)

fetchFile(){

  axios
    .get(`/someurl/thefiles/${this.props.file.id}`, { headers })

    .then(response => {
    
          let url = window.URL.createObjectURL(new Blob([response.data]));
          const link = document.createElement("a");
          link.href = url;
          link.setAttribute("download",
          `${this.props.file.name}.${this.props.file.mime}`);
          document.body.appendChild(link);
          link.click();
  });


}

render(){

  return(
      
      <button onClick={this.fetchFile}> Download file </button>
    
  )

}
<script src="https://cdnjs.cloudflare./ajax/libs/react/16.6.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react-dom/16.6.1/umd/react-dom.production.min.js"></script>

The mime and filename e from a parent ponent.

The problem I have is that I download a file, xlsx for example, and when I open it I get an error box with the message 'The file is damaged', if I download a pdf file, it downloads without problem and when I open the pdf is pletely blank: the same number of sheets as the original but all white.

I am trying from chrome, firefox and brave and the error is the same

Share Improve this question asked Oct 7, 2020 at 20:13 WalWal 431 silver badge6 bronze badges 2
  • 2 Have you looked at this: gist.github./javilobo8/097c30a233786be52070986d8cdb1743? – hexbioc Commented Oct 7, 2020 at 20:41
  • You're the best! Why didn't you put it as an answer? – Wal Commented Oct 7, 2020 at 21:12
Add a ment  | 

1 Answer 1

Reset to default 6

This answer is by @hexebioc

fetchFile(){
   axios({
            url: `/someurl/thefiles/${this.props.file.id}`,
            method: "GET",
            headers: headers,
            responseType: "blob" // important
        }).then(response => {
            const url = window.URL.createObjectURL(new Blob([response.data]));
            const link = document.createElement("a");
            link.href = url;
            link.setAttribute(
                "download",
                `${this.props.file.name}.${this.props.file.mime}`
            );
            document.body.appendChild(link);
            link.click();
        });


}

render(){

  return(
      
      <button onClick={this.fetchFile}> Download file </button>
    
  )

}
<script src="https://cdnjs.cloudflare./ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论