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

I am working with React Js and Axios to make requests to an API.I am trying to download files via API

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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744942482a4602411.html

相关推荐

  • Samsung:揭秘SSD单盘扩容的 IU 方案

    全文概览NAND闪存技术的物理限制以及DRAM成本的考量,使得SSD架构设计面临严峻挑战。如何在有限的DRAM资源下,实现更大容量、更高性能和更长寿命的SSD,成为业界关注的焦点。本文深入探讨了在存储开发者大会(SDC24)上,Samsun

    1小时前
    00
  • 网络协议大梳理:一篇文章串起所有重点

    网络协议大梳理:一篇文章串起所有重点一. 为什么协议那么多因为网络通信 太复杂了:有线、无线、二进制、文本、加密、路由、分包、重传……如果没有标准化协议,每家公司都乱搞,互联网就玩不下去了。 所以我们把复杂通信层层拆分,每层各管一摊,形成了

    1小时前
    00
  • 从沟通压力到智能助手:5 分钟上手构建公益 AI 问答系统

    摘要公益项目经常面临沟通负担重、响应不及时的问题。本文通过一个可运行的 Demo,手把手教你用 LangChain 和 RAG 架构搭建一个 AI 聊天助手,让它来帮你自动回答常见问题、对接知识库,大幅提升沟通效率。引言很多公益组织团队不大

    1小时前
    00
  • docker v28.1.0重磅发布!全新功能升级,网络安全与多平台支持再进化!

    近日,Docker官方发布最新版Docker v28.1.0,为开发者和运维人员带来了众多新特性、重要修复及性能提升。本次版本持续完善容器生态,强化多平台镜像支持,优化网络功能,并同步升级Go语言环境,为Docker的稳定性和扩展性提供坚实

    1小时前
    00
  • 盘点3个流行的C#脚本开源引擎

    推荐3个C#开源脚本引擎,方便我们在项目中,动态执行C#脚本。一、cs-script项目地址:Star:1.7K⭐️CS-Script是非常成熟的C#脚本引擎,自2004年起就发布了,即.NET发布后的两年。支持托管和独立(CLI)执行模型

    1小时前
    00
  • 嵌入式Linux:IO多路复用

    Linux中的IO多路复用是指一种同时监控多个文件描述符的机制,允许程序在不阻塞的情况下等待多个IO事件。IO多路复用主要通过select、poll和epoll这三种系统调用来实现,应用程序可以监视多个文件描述符的状态变化,如读、写或

    1小时前
    00
  • 芭比风AI玩偶席卷全网:ChatGPT几分钟打造你的时尚分身!

    新智元报道编辑:英智【新智元导读】一股由ChatGPT引爆的AI玩偶热潮正在席卷全球社交媒体!从领英到TikTok,人人都在将自己变成可爱玩偶,搭配个性配饰,装进精美包装盒。你准备好了吗?OpenAI又一次吸引了公众的关注。最近,社交媒

    1小时前
    00
  • React Native 调查显示新架构之路坎坷

    作者 | Tim Anderson 译者 | Sambodhi 策划 | TinaReact Native 最新现状调查显示,去年宣布稳定的新架构,尽管是人们期待已久的,但仍然是许多开发人员的痛点。今年的调查从 2024 年 1

    1小时前
    00
  • 新思科技Synopsys白皮书:AI数据中心网络技术的演进

    作者:Jerry Lotto以太网是互联网的核心技术,在数据中心网络中发挥着关键作用。本文阐述了以太网标准的发展历程,探讨了高性能计算(HPC)的演进及其对网络的需求,强调了 SoC 设计师对超越 IP 的以太网解决方案的需求,以及 Syn

    1小时前
    00
  • 淘宝海外暴走的背后,是&amp;quot;川普悖论&amp;quot;的实际代价

    淘宝海外"暴走"之前美区 TikTok 被封,泼天流量给到了海外小红书。这次川普搞"对等关税",泼天流量给到了海外淘宝。由于这次"对等关税",祸害的不止是美国人民,影响面覆盖全球,

    1小时前
    00
  • Python比特币价格时间序列:LGBMRegressor递归自回归、随机游走及外部变量预测探索

    原文链接:tecdat?p=41630在当今数据驱动的时代,数据科学家们肩负着从海量数据中挖掘有价值信息、解决实际问题的重任。我们曾参与一个为客户提供的咨询项目,在这个项目中,我们致力于通过时间序列分析和建模的方法,对某一重要金融资产(比

    1小时前
    00
  • 设计模式:建造者设计模式(Builder)(2)

    关于该设计模式在 VFP 中的应用示例,我考虑了很久。最终决定以主程序的伪代码来做。它至少有两个作用:1. 通过“主程序”这个过程化代码的重灾区的改造,体会 OOP 的设计思想;2. 通过主程序的演变说明该模式的特点。一个最简单的主程序,基

    1小时前
    00
  • Java Maven Settings配置参考

    介绍快速概览settings.xml文件中的 settings 元素包含用于定义以各种方式配置Maven执行的值的元素,如pom.xml,但不应绑定到任何特定项目或分发给受众。这些值包括本地仓库位置、备用远程仓库服务器和身份验证信息。set

    40分钟前
    00
  • Linux:简单指令(二)

    要么对内容要么对属性操作决定路径开始定位文件,也可以相对路径家目录man ~~ 1查询具体命令我们可以man man 可以看man 的描述我们可以man 数字 ~~ 可以从上到下查询2查询仿命令3查询具体接口 man 3 printfm

    25分钟前
    00
  • 字符串系列一>最长回文子串

    题目: 链接: link在这里插入图片描述解析: 这里是引用代码:代码语言:javascript代码运行次数:0运行复制class Solution {public String longestPalindrome(String s) {c

    23分钟前
    00
  • ​​​​​48days强训——day12

    第一题:删除公共字符链接: 描述输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”输入描述:每个测试输入

    20分钟前
    00
  • ChatUI vs Ant Design X 技术选型对比

    引言本文从核心功能、架构设计、易用性等维度对比分析阿里巴巴的 ChatUI 和 Ant Design 的 Ant Design X,帮助开发者选择适合的对话式 UI 开发方案。核心功能对比维度ChatUIAnt Design X定位聚焦对话

    17分钟前
    00
  • 【文件操作与IO】详细解析文件操作与IO (一)

    一. 什么是文件1. 认识文件先来认识狭义上的文件(file)。针对硬盘这种持久化存储的IO设备,当我们想要进行数据保存时,往往不是保存成一个整体,而是独立成一个个的单位进行保存,这个独立的单位就被抽象成文件的概念,就类似办公桌上的一份份

    11分钟前
    00
  • Win11 22H2跳过联网激活的四种实用方法

    Win11 22H2跳过联网激活的四种实用方法 引言 自Windows 11发布以来,微软强化了系统激活机制,要求用户在首次启动时必须联网完成账户登录或激活流程。这一改动虽然提升了系统安全性,但也给需要离线验机或避免激活后影响退换货政策

    5分钟前
    00
  • 又被AI淘汰了!一句话生成自动化操作

    现在谁还手动操作浏览器,应该学会让AI做你自己的打工人,释放自己的双手去,你的手应该是用来摸鱼而不是做这些费时并且无聊的操作代码语言:javascript代码运行次数:0运行复制地址:现在我们只需要通过人类的语言也就是自然语言告诉AI,帮

    4分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信