vue + element 想要自定义上传按钮 想要传多张图片自己改把
export function getFileType(filePath) {
var startIndex = filePath.lastIndexOf(".");
if (startIndex != -1)
return filePath.substring(startIndex + 1, filePath.length).toLowerCase();
else return "";
}
export function postfile(params) {
return request({
url: '/wms/StockIn/inexcel',
method: 'post',
//headers: {'Content-Type':'multipart/form-data'},
data: params
})
}
<el-button type="primary" style="position: relative;overflow: hidden;margin-right: 60px;">导入EXCEL
<div id="clone_input">
<input style="position: absolute;top: 0;left: 0;width: 120px;height: 50px;opacity: 0;" type="file" @change="importf"/>
</div>
</el-button>
import {
getFileType,
getshowexcel,
postfile
} from '@/api/importfiles/index.js'
created() {
this.$nextTick(() => {
window.vm=this
document.getElementById("clone_input").innerHTML=''
this.$nextTick(()=>{
var creatdom=document.createElement("input");
var att=document.createAttribute("style");
att.value="position: absolute;top: 0;left: 0;width: 120px;height: 50px;opacity: 0;";
creatdom.setAttributeNode(att);
var acc=document.createAttribute("type");
acc.value="file";
creatdom.setAttributeNode(acc);
creatdom.addEventListener('change',()=>{
window.vm.importf(creatdom)
} )
document.getElementById("clone_input").appendChild(creatdom)
})
})
},
importf(file) {
console.log(file,11111111111111111)
var filename=getFileType(file.files[0].name)
if(filename!='xlsx'){
this.$message.error('请上传XLSX文件')
return
}
var formdata = new FormData() // 创建一个表单
formdata.append('dbname', 'finance_subject_data')
formdata.append('files', file.files[0])
// 调用接口传文件
postfile(formdata).then(res => {
if(res.success){
this.$message.success('文件上传成功')
this.fetchData()
}
}).finally(()=>{
document.getElementById("clone_input").innerHTML=''
this.$nextTick(()=>{
var creatdom=document.createElement("input");
var att=document.createAttribute("style");
att.value="position: absolute;top: 0;left: 0;width: 120px;height: 50px;opacity: 0;";
creatdom.setAttributeNode(att);
var acc=document.createAttribute("type");
acc.value="file";
creatdom.setAttributeNode(acc);
creatdom.addEventListener('change',()=>{
window.vm.importf(creatdom)
} )
document.getElementById("clone_input").appendChild(creatdom)
})
})
}