当前位置:网站首页>如何通过antd的upload控件,将图片以文件流的形式发送给服务器
如何通过antd的upload控件,将图片以文件流的形式发送给服务器
2022-07-04 08:16:00 【zwf193071】
前言
这几天,遇到个需求,要求将图片以文件流的形式发送给服务器,接口文档内容如下所示:
请求参数 picurl: 2020/9/10/jrg00002.jpg
file:jrg00002.jpg
很明显,file
参数为文件流
对象,那么如何将图片上传给服务器呢
FormData
文件必须以文件流的形式上传给服务器,而文件流的形式有多种,比如Blob
、FormData
等,这里我们选择了FormData
,ui
组件选择了antd
,核心代码如下所示
<template>
<a-upload name="file" list-type="picture-card" class="avatar-uploader" :multiple="true" :show-upload-list="false" :before-upload="beforeUpload" :file-list="fileList" @preview="handlePreview" @change="handleChange" >
<img v-if="item.value" :src="item.value" alt="avatar" />
<div v-else>
<a-icon :type="loading ? 'loading' : 'plus'" />
</div>
</a-upload>
</template>
<script> export default {
methods:{
handleChange(info) {
const file = info.file this.loading = true; const myDate = new Date() const year = myDate.getFullYear() const month = myDate.getMonth() + 1 const day = myDate.getDate(); const formData = new FormData(); formData.append('file', file.originFileObj); formData.append('picurl', `${
year}/${
month}/${
day}/${
file.name}`); formData.append('adcode', this.adcode) uploadPhotoImg(formData).then(res => {
console.log(res) }).finally(_=> {
this.loading = false }) } } } </script>
注意以下两点
info.file
非原始File
文件,其originFileObj
属性值才是原始File
文件,使用formData.append('file', file.originFileObj)
即可以文件流的形式传递给后台ajax
请求的头部Content-Type
需为multipart/form-data
或其他后台接口约定的形式,但一定不是application/json
形式
边栏推荐
- PHP converts seconds to timestamps - PHP
- SQL注入测试工具之Sqli-labs下载安装重置数据库报错解决办法之一(#0{main}thrown in D:\Software\phpstudy_pro\WWW\sqli-labs-……)
- How to use MOS tube to realize the anti reverse connection circuit of power supply
- 如何用MOS管来实现电源防反接电路
- Leetcode 23. 合并K个升序链表
- 21 examples of strategic goals to promote the rapid development of your company
- ZABBIX 5.0 monitoring client
- Difference between static method and non static method (advantages / disadvantages)
- Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
- 一文了解數據异常值檢測方法
猜你喜欢
ctfshow web255 web 256 web257
snipaste 方便的截图软件,可以复制在屏幕上
yolov5 xml数据集转换为VOC数据集
墨者学院-PHPMailer远程命令执行漏洞溯源
Comprendre la méthode de détection des valeurs aberrantes des données
zabbix 5.0监控客户端
Moher college phpMyAdmin background file contains analysis traceability
Redis sentinel mechanism
1. Getting started with QT
Collections in Scala
随机推荐
deno debugger
deno debugger
L1-025 positive integer a+b (15 points)
Activiti常見操作數據錶關系
Flask 常用组件
[test de performance] lire jmeter
Preliminary study on temporal database incluxdb 2.2
[go basics] 1 - go go
OKR vs. KPI figure out these two concepts at once!
Need help resetting PHP counters - PHP
How to write a summary of the work to promote the implementation of OKR?
Thesis learning -- time series similarity query method based on extreme point characteristics
Oracle-存储过程与函数
Linear algebra 1.1
PCIE知识点-010:PCIE 热插拔资料从哪获取
猜数字游戏
[go basics] 2 - go basic sentences
Oracle stored procedures and functions
Example analysis of C # read / write lock
力扣今日题-1200. 最小绝对差