记录两个react下载的方法

这篇博客介绍了在React中实现文件下载的两种方法。第一种方法通过创建隐藏的form元素并模拟提交来调用接口下载。第二种方法则是利用a标签的click事件触发下载,直接在JS中设置href和download属性。这两种方法都可以在不经过浏览器网络面板的情况下完成接口调用和参数传递。
摘要由CSDN通过智能技术生成

第一种方法,可以在network查看到接口的调用和请求体携带的参数

import ReactDOM from ‘react-dom’

exportFn = ()=>{
let exportStr
exportStr = 下载当前文件的ID
this.exporUtilModel(exportStr)//调用下载方法

}

exporUtilModel =(exportStr)=>{

Iet divElement = document.getElementById(‘downloadDiv’)//虚拟个div

let path = ‘xxxxxxxxx’//下载所需要的接口

React.render(

<form action={path) method=‘post’>//把接口和请求方式填写一下

<input name=‘id’ type=‘text’ value={exportStr}</input>

</form>,

divElement)

ReactDom.findDOMNode(divElement).quertSelector(‘form’).submit()//找到新建的div,提交它

ReactDom.unmountComponentAtNode(divElement)//用过即弃

}

在jsx里要写<div id='divElement' ></div>

第二种方法 ,在network上面框看不见接口调用和参数

exportfn=(donwloadID)=>{

//输入进来的参数是要下载的数据的唯一id

const a= document.createElement(‘a’)//创建一个标签

let path = ‘xxxxxx?id’+downloadID//接口后面拼接参数

a.setAttribute(‘download’,’’)//给元素属性赋值

a.setAttribute(‘href’,path)//给元素赋值

a.click()//给点击事件

}

在公司下班无聊手敲的,可能会有拼写错误,劳烦各位大佬批评指正了~

Learning React: A Hands-on Guide to Building Maintainable, High-Performing Web Application User Interfaces Using the React JavaScript by Kirupa Chinnathambi English | 15 Nov. 2016 | ISBN: 0134546318 | 240 Pages | AZW3/MOBI/EPUB/PDF (conv) | 47.52 MB A hands-on guide to building maintainable, high-performing web application user interfaces using the React JavaScript library As far as new web frameworks and libraries go, React is quite the runaway success. It not only deals with the most common problems developers face when building complex apps, it throws in a few additional tricks that make building the visuals for such apps much, much easier. What React isn’t, though, is beginner-friendly and approachable. Until now. In Learning React, author Kirupa Chinnathambi brings his fresh, clear, and very personable writing style to help you understand the fundamentals of React and how to use it to build really performant (and awesome) apps. Build your first React app Create components to define parts of your UI Combine components into other components to build more complex UIs Use JSX to specify visuals without writing full-fledged JavaScript Deal with maintaining state Work with React’s way of styling content Make sense of the mysterious component lifecycle Build multi-page apps using routing and views Optimize your React workflow using tools such as Node, Babel, webpack, and others Contents at a Glance Chapter 1 Introducing React Chapter 2 Building Your First React App Chapter 3 Components in React Chapter 4 Styling in React Chapter 5 Creating Complex Components Chapter 6 Transferring Properties (Props) Chapter 7 Meet JSX–Again! Chapter 8 Dealing with State Chapter 9 Going from Data to UI Chapter 10 Working with Events Chapter 11 The Component Lifecycle Chapter 12 Accessing DOM Elements Chapter 13 Creating a Single-Page App Using React Router Chapter 14 Building a Todo List App Chapter 15 Setting Up Your React Development Environment Chapter 16 The End
Over 66 hands-on recipes that cover UI development, animations, component architecture, routing, databases, testing, and debugging with React Today’s web demands efficient real-time applications and scalability. If you want to learn to build fast, efficient, and high-performing applications using React 16, this is the book for you. We plunge directly into the heart of all the most important React concepts for you to conquer. Along the way, you’ll learn how to work with the latest ECMAScript features. You’ll see the fundamentals of Redux and find out how to implement animations. Then, you’ll learn how to create APIs with Node, Firebase, and GraphQL, and improve the performance of our application with Webpack 4.x. You’ll find recipes on implementing server-side rendering, adding unit tests, and debugging. We also cover best practices to deploy a React application to production. Finally, you’ll learn how to create native mobile applications for iOS and Android using React Native. By the end of the book, you’ll be saved from a lot of trial and error and developmental headaches, and you’ll be on the road to becoming a React expert. What You Will Learn Gain the ability to wield complex topics such as Webpack and server-side rendering Implement an API using Node.js, Firebase, and GraphQL Learn to maximize the performance of React applications Create a mobile application using React Native Deploy a React application on Digital Ocean Get to know the best practices when organizing and testing a large React application
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值