vue上传文件
2024-10-10 22:31:39
<div>
<input type="file" class="file" name="file" @change="uploadBefore">
</div>
export default {
methods:{
//选择文件
uploadBefore(){
var formData = new FormData() // 声明一个FormData对象
this.formData = new window.FormData() // vue 中使用 window.FormData(),否则会报 'FormData isn't definded'
this.formData.append('file', document.querySelector('input[type=file]').files[0]) // 'userfile' 这个名字要和后台获取文件的名字一样;
let file = document.querySelector('input[type=file]').files[0]
console.log(this.fileName)
let fileName = file.name.substring(file.name.lastIndexOf(".")+1,file.name.length)
const fileType = fileName == 'xls'||fileName == 'xlsx'||fileName == 'ppt'||fileName == 'pptx'||fileName == 'doc'||fileName == 'docx'||fileName == 'txt'||fileName == 'pdf'||fileName == 'jpg'||fileName == 'gif'||fileName == 'jpeg'||fileName == 'png'||fileName == 'bmp'||fileName == 'mp3'||fileName == 'mp4';
// console.log(file)
// console.log(fileName)
// console.log(fileType)
if (!fileType) {
this.$message.error('上传文件格式不正确,请检查');
this.numberValidateForm.fileName = ""
}else{ this.numberValidateForm.fileName = file.name
}
},
// 保存,上传文件
submitForm() {
this.saveLoading = true
this.$http({
url: this.$store.state.domian+'/bbt-admin/courseware/upload/'+dynamicppt,
data: this.formData,
method: 'post',
headers: {
'Content-Type': 'multipart/form-data',
}
}).then((res => {
if (res.data.statusCode == 0) { this.$message({message:'操作成功!',type: 'success'})
this.dialogTableVisible = false
this.getFileList(1); }
this.saveLoading = false
console.log(res)
})
) });
},
} }
最新文章
- linux vps安装kloxo配置全部过程
- C++函数返回局部指针变量
- Python:if __name__ == &#39;__main__&#39;
- iOS之UI--指示器HUD的创建和设置
- Acdream1217 Cracking&#39; RSA(高斯消元)
- UVa 11426 (欧拉函数 GCD之和) GCD - Extreme (II)
- struts1面试题
- View的工作原理(一)——Measure
- [转载] 关于Windows Boot Manager、Bootmgfw.efi、Bootx64.efi、bcdboot.exe 的详解
- Images as x-axis labels
- Git &; github 使用指南
- 关于 pyspider Web预览界面太小的解决方法
- CMDB-(paramiko模块 -- 实现ssh连接)
- 【转】CentOS 7部署ASP.NET Core应用程序
- node 各模块及对应功能
- RESET MASTER和RESET SLAVE使用场景和说明【转】
- 深入理解$watch ,$apply 和 $digest --- 理解数据绑定过程——续
- 1692. [USACO07DEC] 队列变换【后缀数组+贪心】
- Controller层@PathVariable使用
- DevExpress 行事历(Scheduler)的常用属性、事件和方法