当前位置:网站首页>如何通过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
形式
边栏推荐
- 如何用MOS管来实现电源防反接电路
- 21个战略性目标实例,推动你的公司快速发展
- 【Go基础】2 - Go基本语句
- Redis sentinel mechanism
- zabbix監控系統自定義監控內容
- Unity text superscript square representation +text judge whether the text is empty
- deno debugger
- Ecole bio rushes to the scientific innovation board: the annual revenue is 330million. Honghui fund and Temasek are shareholders
- Oracle stored procedures and functions
- 小程序容器技术与物联网 IoT 可以碰撞出什么样的火花
猜你喜欢
1、卡尔曼滤波-最佳的线性滤波器
Email alarm configuration of ZABBIX monitoring system
[go basics] 1 - go go
C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码
ctfshow web255 web 256 web257
Linear algebra 1.1
Fault analysis | MySQL: unique key constraint failure
Newh3c - routing protocol (RIP, OSPF)
论文学习——基于极值点特征的时间序列相似性查询方法
Project 1 household accounting software (goal + demand description + code explanation + basic fund and revenue and expenditure details record + realization of keyboard access)
随机推荐
Do you know about autorl in intensive learning? A summary of articles written by more than ten scholars including Oxford University and Google
Leetcode 146. LRU 缓存
Unity-Text上标平方表示形式+text判断文本是否为空
L1-024 the day after tomorrow (5 points)
What determines vacuum permittivity and vacuum permeability? Why do these two physical quantities exist?
L1-027 rental (20 points)
Moher college phpMyAdmin background file contains analysis traceability
Mouse over to change the transparency of web page image
PCIe knowledge points -010: where to get PCIe hot plug data
NewH3C——ACL
2022 gas examination registration and free gas examination questions
OpenFeign 服务接口调用
【Go基础】2 - Go基本语句
Put a lantern on the website during the Lantern Festival
The second session of the question swiping and punching activity -- solving the switching problem with recursion as the background (I)
ES6 summary
Thesis learning -- time series similarity query method based on extreme point characteristics
Example analysis of C # read / write lock
How to write a summary of the work to promote the implementation of OKR?
A single element in an ordered array