当前位置:网站首页>如何通过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
形式
边栏推荐
- Set and modify the page address bar icon favicon ico
- 线性代数1.1
- 21个战略性目标实例,推动你的公司快速发展
- Do you know about autorl in intensive learning? A summary of articles written by more than ten scholars including Oxford University and Google
- 运动【跑步 01】一个程序员的半马挑战:跑前准备+跑中调整+跑后恢复(经验分享)
- Flask 常用组件
- Leetcode 23. 合并K个升序链表
- Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future
- 广和通高性能4G/5G无线模组解决方案全面推动高效、低碳智能电网
- @Role of requestparam annotation
猜你喜欢
Do you know about autorl in intensive learning? A summary of articles written by more than ten scholars including Oxford University and Google
NewH3C——ACL
Project 1 household accounting software (goal + demand description + code explanation + basic fund and revenue and expenditure details record + realization of keyboard access)
Linear algebra 1.1
墨者学院-PHPMailer远程命令执行漏洞溯源
Newh3c - routing protocol (RIP, OSPF)
zabbix监控系统自定义监控内容
Sports [running 01] a programmer's half horse challenge: preparation before running + adjustment during running + recovery after running (experience sharing)
L1-027 rental (20 points)
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
随机推荐
snipaste 方便的截图软件,可以复制在屏幕上
L1-027 rental (20 points)
[go basics] 2 - go basic sentences
Moher college phpMyAdmin background file contains analysis traceability
Heap concept in JVM
Oracle stored procedures and functions
OKR vs. KPI figure out these two concepts at once!
DM8 database recovery based on point in time
【性能测试】一文读懂Jmeter
Leetcode (215) -- the kth largest element in the array
Unity write word
C#实现一个万物皆可排序的队列
[Gurobi] 简单模型的建立
R language uses cforest function in Party package to build random forest based on conditional inference trees, uses varimp function to check feature importance, and uses table function to calculate co
Fault analysis | MySQL: unique key constraint failure
Div hidden in IE 67 shows blank problem IE 8 is normal
Easy to understand: understand the time series database incluxdb
Leetcode 23. 合并K个升序链表
PHP converts seconds to timestamps - PHP
Question 49: how to quickly determine the impact of IO latency on MySQL performance