首页 > 其他分享 >Vue—实现文件上传(多文件),图片上传

Vue—实现文件上传(多文件),图片上传

时间:2023-12-11 15:13:46浏览次数:27  
标签:文件 Vue console log formData result file 上传

一、图片上传
//template
<input
type="file"
accept="image/*"
@change="changeImage()"
ref="avatarInput"
style="display:none"
/>
<div class="pic_list">
<div v-for="(src, index) in imgDatas" :key="index">
<!-- 利用element-ui的图片预览插件 -->
<el-image
style="margin-right: 10px;width: 100px; height: 100px"
:src="src"
:preview-src-list="imgDatas"
>
</el-image>
</div>
<!-- 替换自己的上传图标 -->
<p class="upload_btn" @click="upLoad">+</p>
</div>
//script
data(){
return {
formData: new FormData(),
imgDatas:[]
}
},
methods:{
changeImage() {
// 上传图片事件
var files = this.$refs.avatarInput[0].files;
var that = this;
if (files.length === 0) {
return;
}
function readAndPreview(file) {
//Make sure `file.name` matches our extensions criteria
if (/\.(jpe?g|png|gif)$/i.test(file.name)) {
var reader = new FileReader();
reader.onload = function(e) {
// 防止重复上传
if (that.imgDatas.indexOf(e.target.result) === -1) {
that.imgDatas.push(e.target.result);
that.formData.append("imgFile", file);
// console.log(that.formData.getAll("imgFile"));
}
};
reader.readAsDataURL(file);
}
}
if (files.length !== 0) {
readAndPreview(files[0]);
}
// console.log(this.formData.getAll("imgFile"));
// 文件上传服务器
// this.setUploadFile()
},
setUploadFile() {
// this.$http
// .post("/api/dxbase/upload?resType=EVENT", this.formData)
// .then(res => {
// console.log(res);
// });
},
upLoad() {
// 触发上传图片按钮
this.$refs.avatarInput[0].dispatchEvent(new MouseEvent("click"));
}
}
//style

.upload_btn {
width: 100px;
height: 100px;
background: #fbfdff;
border: 1px dashed #999;
color: #666;
box-sizing: border-box;
font-size: 40px;
text-align: center;
line-height: 100px;
cursor: pointer;
}

.pic_list {
display: flex;
flex-wrap: wrap;
}

二、文件上传
<el-button @click="choseFile" type="primary"
>选择上传文件</el-button
>
<p>
<span
v-for="(fileItem, fileIndex) in fileData"
:key="fileIndex"
style="font-size:12px;color: #666;padding-left: 10px;"
>{{ fileItem.name + ";" }}</span
>
</p>
<input
id="fileInput"
style="display:none;"
type="file"
@change="addFile"
ref="inputer"
/>
</div>
//script
data(){
return {
formData: new FormData(),
file: {},
fileData: [],
}
},methods:{
choseFile() {
// 选择文件
let fileInput = document.getElementById("fileInput");
fileInput.click();
},
addFile() {
// 添加文件
let inputDOM = this.$refs.inputer[0];
if (inputDOM.files[0]) {
this.file = inputDOM.files[0];
// 防止重复上传
//Make sure `file.name` matches our extensions criteria

var reader = new FileReader();
reader.onload = e => {
// console.log(this.fileData);
// console.log(
// this.fileData.some(v => {
// return v.result !== e.target.result;
// })
// );
if (this.fileData.length == 0) {
this.fileData.push({
name: this.file.name,
result: e.target.result
});
this.formData.append("file", this.file);
// console.log(this.formData.getAll("file"));
} else {
// 防止重复上传
if (
!this.fileData.some(v => {
return v.result == e.target.result;
})
) {
this.fileData.push({
name: this.file.name,
result: e.target.result
});
this.formData.append("file", this.file);
// console.log(this.formData.getAll("file"));
}
}
//this.submitFile();
};
reader.readAsDataURL(this.file);
}
// console.log(this.formData.getAll("file"));
// console.log(this.file)
},
submitFile() {
//this.$http
// .post("/api/dxbase/upload?resType=EVENT", this.formData)
// .then(res => {
// console.log(res);
// });
}
}

 

参考文章:http://blog.ncmem.com/wordpress/2023/12/11/vue-%e5%ae%9e%e7%8e%b0%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0%ef%bc%88%e5%a4%9a%e6%96%87%e4%bb%b6%ef%bc%89%ef%bc%8c%e5%9b%be%e7%89%87%e4%b8%8a%e4%bc%a0/

欢迎入群一起讨论

 

 

标签:文件,Vue,console,log,formData,result,file,上传
From: https://www.cnblogs.com/songsu/p/17894455.html

相关文章

  • GitLab部署vue项目至GitLab Pages流水线配置
    image:node:16.15.0#表示使用有nodejs环境的docker,自己项目用的什么版本这就写什么版本stages:-deploypages:#!!!必须!!!使用pages关键字stage:deployscript:-npminstall-npmrunbuild-rm-rfpublic#删除public目录,开发过......
  • peony打开文件命令
    /***麒麟kylin使用命令调用文件管理器打开指定路径的方法*优麒麟早期版本(16.04前)使用Nautilus命令,后来版本使用peony命令*语法如下:*peonyfile://<路径>*例如:*peonyfile:///home/liumou/*查找文件命令*find/-namereadme.doc*创建目录命令*mkdir文件夹......
  • 解读传奇补丁什么是Pak文件什么是WIL序列号
    Pak文件是GOM引擎自定义图片资源格式,支持密码功能,可以使用工具包中的WIL编辑器创建修改等编辑,很多脚本命令和功能都会使用这个WIL序号,M2-查看-列表信息二这里可以自定义添加,Pak文件读取规则详细查看登录器配置器Pak文件读取规则和Pak密码需要在登录器配置中配置。什么是Pak文件Pa......
  • linux 多线程写同一个文件
    来自:https://blog.popkx.com/linux-multithreaded-programming-in-io-read-write-security-functions-pread-pwrite-and-read-write-what-is-the-difference-and-relat/ #include<unistd.h>ssize_tpread(intfd,void*buf,size_tcount,off_toffset);ssize_t......
  • [Python急救站]文件管理工具
    对于一个程序员,有时候文件太多,忘记放哪里了,那有没有一个可以帮你定位到文件的文件管理工具呢,抱着这样的想法,我做了以下这个代码,可以快速定位找到文件所在位置。importosimporttkinterastkimporttimeimportsubprocess#函数用于搜索文件defsearch_files():file......
  • 分享一个 asp.net core 多文件分块同时上传的组件
    分享一个可多个文件同时上传、断点续传,并实时反馈上传进度的Asp.Netcore组件。服务器端引用nuget包:JMS.FileUploader.AspNetCore然后启用上传组件:app.UseAuthorization();app.MapControllers();//启用上传组件,并限制单个文件最......
  • Springboot+Vue实现多文件上传
    多文件上传,后端接收到多次请求vue实现<el-uploadclass="upload-demo"action="http://10.240.46.88:8081/upload1":on-preview="handlePreview":on-remove="handleRemove":multiple="multiple"......
  • 关键字 开发-11 yaml文件中添加fixture功能
    前言每一条pytest用例都可以添加fixture的前置功能,只需要在用例的参数中传入在conftest.py文件中写的函数的名称就可以实现每条用例的前置和后置。1.yaml文件中加入fixture的功能因为我们实现了动态生成测试用例,所以我们可以直接通过在生成动态用例的时候就传入该fixture函数......
  • Linux下删除当前目录下的所有文件夹及文件保留最新的几个文件夹及文件
    一、查找目录或文件1.1、查找指定文件夹和文件具体的示例:查找当前目录下指定文件夹和文件find./-maxdepth1-name"jobs"-o-name"config.xml"命令说明-maxdepth目录深度,1表示只搜索一级目录-name后面跟文件夹或文件,多个文件夹或文件,用-o组合连接jobs、config.xml指定的文......
  • 文件分片和断点虚传
    前端代码要优化前端的文件分片上传代码,我们可以考虑以下几点:异步上传与并发控制:上传分片时使用异步请求,并控制同时上传的分片数量,避免同时发送过多请求导致浏览器或服务器压力过大。上传进度显示:向用户显示每个分片的上传进度和总体进度。断点续传:在上传之前检查哪些分片已经上传过......