axios

前端JS 下载大文件解决方案

孤者浪人 提交于 2020-07-28 19:41:03
问题场景 点击导出按钮,提交请求,下载excel大文件(超过500M),该文件没有预生成在后端, 直接以文件流的形式返回给前端。 解决方案 在Vue项目中常用的方式是通过axios配置请求,读取后端返回的文件流,常用代码如下: axios({ method: 'post', url: 'api/file', responseType: 'blob' }).then(res=> { if (res.data){ filename = 'filename'; let blob = new Blob([res.data],{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8"}); if (window.navigator.msSaveOrOpenBlob){ // IE10+下载 navigator.msSaveOrBlob(blob, filename); }else{ // 非IE10+下载 let link = document.createElement('a'); link.href = window.URL.createObjectURL(blob); link.download = filename; document.body.appendChild

vue~使用axios实现excel文件下载的实现

时光毁灭记忆、已成空白 提交于 2020-07-28 17:19:27
前端VUE页面上的导出或者下载功能,一般是调用后端的一个接口,由接口生成excel,word这些文件的流信息,返回给vue,然后由vue去构建下载的动作,这边整理了一下,封装了一下,方便以后复用。 封装一个download文件 使用年月日时分秒毫秒做为文件的名称,下载为excel文件 /** * 下载文件 */ export const downloadFile = (url,ext, params) => { let accessToken = getStore('accessToken'); return axios({ method: 'get', url: `${base}${url}`, params: params, headers: { 'accessToken': accessToken }, responseType: 'blob', //二进制流 }).then(res => { // 处理返回的文件流 const content = res; const blob = new Blob([content], { type: 'application/vnd.ms-excel;charset=utf-8' }); var date = new Date().getFullYear() + "" + (new Date().getMonth() + 1) + ""

node连接mysql生成接口,vue通过接口实现数据的增删改查(一)

自古美人都是妖i 提交于 2020-07-28 12:14:25
  武汉加油!中国加油!   想必许多学vue的小伙伴想连接数据库,对数据进行增删改查吧,奈何不知道怎么实现。作为一路踩坑的我,为大家带来我的一些踩坑经历,水平有限,其中错误,望请指正。    前言:   本篇主要讲述的是如何把零件凑在一起让车跑起来,不会去关注如何制造零件,等车跑起来了我们再去了解造零件。   先看一下效果图,如果是你需要的再往下看,不是则不必浪费你的时间。如图1,图2所示 图1 添加信息图 图2 查询、修改信息图 一、所需环境   node、mysql、vue,最好是有phpstudy,其自带mysql,非常方便。如此选择是因为,node是运行在服务端的 JavaScript,速度快,性能好,mysql速度快,安装方便且轻量。 二、搭建项目 2.1 搭建vue项目   我是用的是v-cli4.0的脚手架进行搭建的,不是本文重点,不详细介绍。 2.1.1 安装依赖 cnpm install vuex --save cnpm install axios --save   在mian.js中挂载原型。如图3所示。 图3 2.1.2 安装服务依赖 cnpm install mysql express body-parser --save 2.2 搭建本地服务   搭建完成vue项目后,在根目录建立server文件夹;   在server下建立api文件夹

springboot+vue项目大型实战(一)后端开发

夙愿已清 提交于 2020-07-28 03:57:43
数据库创建表 SET NAMES utf8mb4 ; SET FOREIGN_KEY_CHECKS = 0 ; -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Table structure for book -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- DROP TABLE IF EXISTS `book` ; CREATE TABLE `book` ( `id` int ( 10 ) NOT NULL , `name` varchar ( 20 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , `author` varchar ( 20 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , `publish` varchar ( 20 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , `pages` int ( 10 ) NULL DEFAULT NULL , `price` float ( 10 , 2 ) NULL DEFAULT NULL ,

配置七牛云上传图片

穿精又带淫゛_ 提交于 2020-07-28 01:52:46
配置七牛云上传图片 在七牛云/对象存储/空间管理下,新建空间,记下空间名 进入新建空间,打开域名管理,绑定自己的域名如(img.xxxx.com) 进入管理域名的地方添加域名解析,记录类型选择CNAME 选择熟悉的语言,安装sdk,书写token获取接口 php composer.phar require qiniu/php-sdk /** * 获取七牛token * @return Json */ public function getToken() { $accessKey = '<AK>'; $secretKey = '<SK>'; $bucket = '<空间名>'; // 生成上传Token $auth = new Auth($accessKey, $secretKey); $token = $auth->uploadToken($bucket); return $this->json_responce(['token' => $token]); } 前台获取接口上传图片,然后返回链接 data () { return { imageUrl: '', // 七牛云的上传地址,根据自己所在地区选择,我这里是华南区 domain: 'https://upload-z2.qiniup.com', // 这是七牛云空间的外链默认域名 qiniuaddr: 'img.xxxx

Not Receiving Set-Cookie Header with axios post request

折月煮酒 提交于 2020-07-28 00:21:15
问题 I have a PHP Script which successfully returns some simple Headers as well as a set-cookie header if called directly in the browser (or by postman). I can read the response-headers like that from chrome devTools. But as soon as I call it by Axios, the set-cookie header doesn't show up and there's no cookie saved in the browser. I tried diffrent things like changing the response-headers server-side and using "withCredentials: true" with axios, but nothing worked. I don't even get an error or

Not Receiving Set-Cookie Header with axios post request

时间秒杀一切 提交于 2020-07-28 00:18:13
问题 I have a PHP Script which successfully returns some simple Headers as well as a set-cookie header if called directly in the browser (or by postman). I can read the response-headers like that from chrome devTools. But as soon as I call it by Axios, the set-cookie header doesn't show up and there's no cookie saved in the browser. I tried diffrent things like changing the response-headers server-side and using "withCredentials: true" with axios, but nothing worked. I don't even get an error or

发axios请求,后端接收参数为null

纵饮孤独 提交于 2020-07-27 11:45:33
解决方案:前端引入Axios qs。 把要发送的参数对象使用QS.stringify({user:"xxx"}) 转换格式 再发送axios请求 login:function(){ let u = Qs.stringify({user:this.user}); if(this.user==""){ this.tips("账号不能为空") return } axios.post("http://localhost:8080/wws/login",u). then(function(resp){ console.log(resp) }) } 来源: oschina 链接: https://my.oschina.net/u/3702441/blog/4330029

vue+element-ui JYAdmin后台管理系统模板-集成方案【项目搭建篇2】

青春壹個敷衍的年華 提交于 2020-07-27 00:02:03
项目搭建时间:2020-06-29 本章节:讲述基于vue/cli, 项目的基础搭建。 本主题讲述了: 1、跨域配置 2、axios请求封装 3、eslint配置 4、环境dev,test,pro(开发,测试,线上), run自动调用对应的接口(proxy多代理配置) vue+element-ui JYAdmin 后台管理系统模板-集成方案 从零到一的手写搭建全过程。 该项目不仅是一个持续完善、 高效简洁的后台管理系统模板, 还是一套企业级后台系统开发 集成方案,致力于打造一个 与时俱进、高效易懂、高复用、 易维护扩展的应用方案。 1、安装axios cnpm i axios --save    2、axios封装,调用以及api资源管理 @/serve/axiosResquest.js(axios封装) import axios from 'axios'; axios.interceptors.response.use( response => { return response }, error => { if (error && error.response) { const ERR_CODE_LIST = { //常见错误码列表 [400]: "请求错误", [401]: "登录失效或在其他地方已登录", [403]: "拒绝访问", [404]: "请求地址出错",

axios.post(...).then(...).catch(...).finally is not a function解决办法

青春壹個敷衍的年華 提交于 2020-07-26 23:30:54
方式1:手动实现finally() //简单亲测可用 引入的js后加入 <script> Promise.prototype.finally = function (callback) { let P = this.constructor; return this.then( value => P.resolve(callback()).then(() => value), reason => P.resolve(callback()).then(() => { throw reason }) ); }; </script> 方式2:借助 promise.prototype.finally 包 npm install promise-prototype-finally 最后记得在 main.js 里引入该依赖包: const promiseFinally = require('promise.prototype.finally'); // 向 Promise.prototype 增加 finally() promiseFinally.shim(); // 之后就可以按照上面的使用方法使用了 借鉴资料 https://segmentfault.com/a/1190000015550213 https://blog.csdn.net/weixin_41828535/article