使用FormData上传图片
2024-09-08 17:26:22
document.querySelector('.file').addEventListener('change', function(e) {
//1.可以通过this拿到这个file的DOM元素
console.log(this)
//1. e 函数事件参数对象中也有这个file的DOM元素对象。使用e.target也可以直接拿到
console.log(e.target)
//对开发人员屏蔽,所以直接 必须调用这个文件的DOM对象的files属性,返回一个数组
let files = e.target.files
console.log(files)
// console.log(files.item(0))
// console.log(files[0])
// 判断一手是否有文件
if (!files.length) return
// 上传文件 创建FormData
let formData = new FormData()
// upFile就是后台接收的key
formData.append('upFile', files[0], files[0].name)
// 将formdata发送到后台即可
// 我用的 axios.post('url', formData)
})
最新文章
- jvm 之结构与机制
- bash快捷建
- jQuery源代码学习之七—队列模块queue
- .NET生成word文档服务器配置常见问题
- 标签工作区(navtab)
- JS打开摄像头并截图上传
- SSE图像算法优化系列十:简单的一个肤色检测算法的SSE优化。
- _.contains is not a function
- ios video标签部分mp4文件无法播放的问题
- Python内置函数(8)——bool
- MyBatis学习日记(三):戏说MyBatis配置文件
- 自定义element-ui中的图标
- 【.NET 深呼吸】在 .net core app 中使用 Composition
- GBDT学习
- linux列出目录下所有目录
- 关于plot画图的原理
- (转)一次棘手的rootvg更换硬盘处理过程
- A1048
- POJ 2186 Popular Cows(强连通分量Kosaraju)
- VMWare安装Ubuntu及配置开发环境遇到的问题集
热门文章
- webpack核心用法,为什么要使用webpack
- 真·生产力「GitHub 热点速览」
- 【KAWAKO】在windows上用CMake和MinGW编译c++工程
- 题解 P1627 [CQOI2009] 中位数
- fastai fit_one_cycle AttributeError: 'function' object has no attribute 'parameters'
- pg高可用方案repmgr带witness搭建
- Vue props配置项(属性)
- java.sql.SQLException: Access denied for user ‘root‘@‘localhost‘ (using password: YES)问题
- linux 操作命令大全
- How to Use Arrays and Vectors