当前位置:网站首页>如何通过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
形式
边栏推荐
- DM8 database recovery based on point in time
- 如何用MOS管来实现电源防反接电路
- Heap concept in JVM
- [untitled] 2022 polymerization process analysis and polymerization process simulation examination
- zabbix監控系統自定義監控內容
- Redis 哨兵机制
- Need help resetting PHP counters - PHP
- Add log file to slim frame - PHP
- The right way to capture assertion failures in NUnit - C #
- 21个战略性目标实例,推动你的公司快速发展
猜你喜欢
How to improve your system architecture?
Ecole bio rushes to the scientific innovation board: the annual revenue is 330million. Honghui fund and Temasek are shareholders
[CV] Wu Enda machine learning course notes | Chapter 9
Thesis learning -- time series similarity query method based on extreme point characteristics
Easy to understand: understand the time series database incluxdb
Devops Practice Guide - reading notes (long text alarm)
【Go基础】1 - Go Go Go
Take you to master the formatter of visual studio code
广和通高性能4G/5G无线模组解决方案全面推动高效、低碳智能电网
Système de surveillance zabbix contenu de surveillance personnalisé
随机推荐
Li Kou today's question -1200 Minimum absolute difference
猜数字游戏
1、卡尔曼滤波-最佳的线性滤波器
Système de surveillance zabbix contenu de surveillance personnalisé
What sparks can applet container technology collide with IOT
FRP intranet penetration, reverse proxy
[gurobi] establishment of simple model
Using the rate package for data mining
@Role of requestparam annotation
AcWing 244. Enigmatic cow (tree array + binary search)
Sqli labs download, installation and reset of SQL injection test tool one of the solutions to the database error (# 0{main}throw in d:\software\phpstudy_pro\www\sqli labs-...)
Unity-Text上标平方表示形式+text判断文本是否为空
弈柯莱生物冲刺科创板:年营收3.3亿 弘晖基金与淡马锡是股东
Leetcode(215)——数组中的第K个最大元素
Leetcode 146. LRU 缓存
OKR vs. KPI figure out these two concepts at once!
[untitled] 2022 polymerization process analysis and polymerization process simulation examination
Convert datetime string to datetime - C in the original time zone
力扣今日题-1200. 最小绝对差
L1-023 output gplt (20 points)