当前位置:网站首页>el-upload添加请求头
el-upload添加请求头
2022-07-30 22:51:00 【辰九九】
solution
Vue项目,需要上传文件
task
使用elementUI的el-upload组件实现文件的上传
action
代码如下
<el-upload
class="upload-demo"
drag
action="http://47.92.112.6:8000/api/admin/calc/file"
:multiple="false"
:show-file-list="true"
accept=".rar,.zip"
:limit=1
>
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<div class="el-upload__tip" slot="tip">只能上传rar/zip文件</div>
</el-upload>
上传文件报错 出现跨域问题,查看请求头因为没有携带token,(项目中使用jwt作为用户身份凭证)
于是增加请求头,查看官网API,需要增加 headers请求头
修改代码如下,增加 headers
<el-upload
class="upload-demo"
drag
action="http://47.92.112.6:8000/api/admin/calc/file"
:multiple="false"
:show-file-list="true"
accept=".rar,.zip"
:limit=1
:headers="headers"
>
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<div class="el-upload__tip" slot="tip">只能上传rar/zip文件</div>
</el-upload>
computed内增加如下内容
computed: {
headers(){
const jwt=localStorage.getItem('jwt')
return{
jwt:jwt
};
},
获取存储在localStorage中的jwt,加载请求头上
调试,文件上传成功,请求头如下

result
在vue中,computed表示“计算属性”,是根据依赖关系进行缓存的计算,只有在它的相关依赖发生改变时才会进行更新,所以在computed中获取jwt,便于获得最新的数据
以前没有学过Vue,直接上手写项目,多有不熟练之处,敬请指正
边栏推荐
- EasyExcel comprehensive course combat
- PS基础学习(一)
- Introducing the visualization tool Netron
- grub 学习
- WSL安装图形界面并通过xrdp/X-Launch访问
- Compressing Deep Graph Neural Networks via Adversarial Knowledge Distillation
- "Code execution cannot continue because MSVCP140.dll was not found, reinstalling the program may resolve the problem, etc." Solutions
- Week 19 Progress (Understanding IoT Basics)
- Jetson AGX Orin 平台关于c240000 I2C总线和GMSL ses地址冲突问题
- 网安学习-内网渗透3
猜你喜欢
随机推荐
The most powerful and most commonly used SQL statements in history
2022.7.28
EasyExcel comprehensive course combat
【MySQL】Mysql事务以及权限管理
【Untitled】
MySQL连接时出现2003错误
反转链表-头插反转法
Abstract classes and interfaces (study notes)
2022.7.30
TCP 连接 三次握手 四次挥手
Golang go-redis cluster模式下不断创建新连接,效率下降问题解决
打动中产精英群体,全新红旗H5用产品力跑赢需求
Rust编译报错:error: linker `cc` not found
Alibaba Cloud video on demand + project combat
【CTF】buuctf web 详解(持续更新)
When Navicat connects to MySQL, it pops up: 1045: Access denied for user 'root'@'localhost'
PS Basic Learning (1)
Detailed operator
可视化工具Netron介绍
Excel basic study notes








