目录
2、提交方式2--使用el的http-request自定义的action的提交
1、提交方式1--使用el自定义的action的提交
<el-form :model="form" id="upload_form">
<el-upload ref="myupload"
name="file"
action="http://127.0.0.1:8080/xxx-cms/file/uploadFile"
accept="image/png,image/jpg,image/jpeg"
list-type="picture-card"
:limit=2
:auto-upload="false"
:on-exceed="handleExceed"
:before-upload="handleBeforeUpload"
:on-preview="handlePictureCardPreview"
:on-remove="handleRemove"
:on-success="handleAvatarSuccess"
:on-error = 'errorTest'
:on-progress = 'progress'
>
<i class="el-icon-plus"></i>
</el-upload>
<el-button size="small" type="primary" @click="uploadFile">立即上传</el-button>
</el-form>
<script>
import axios from "axios";
export default {
data() {
return {
limitNum: 2,
// nodeFile:'http://127.0.0.1:8080/xxx-cms/file/uploadFile',
nodeFile:'http://10.204.5.9:8070/xxx-cms/file/uploadFile',
image_url:''
}
},
methods: {
// 上传文件之前的钩子
handleBeforeUpload(file) {
console.log('handleBeforeUpload');
if (!(file.type === 'image/png' || file.type === 'image/jpg' || file.type === 'image/jpeg')) {
this.$notify.warning({
title: '警告',
message: '请上传格式为image/png, image/jpg, image/jpeg的图片'
})
}
let size = file.size / 1024 / 1024 / 10;
if (size > 10) {
this.$notify.warning({
title: '警告',
message: '图片大小必须小于10M'
})
}
},
// 文件超出个数限制时的钩子
handleExceed(files, fileList) {
this.$notify.warning({message: '只允许上传一张图片'})
},
// 文件列表移除文件时的钩子
handleRemove(file, fileList) {
console.log(file, fileList);
},
// 点击文件列表中已上传的文件时的钩子
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url;
this.dialogVisible = true;
},
//自带的上传上传成功的回调方法
handleAvatarSuccess(response, file, fileList) {
console.log("handleAvatarSuccess-------");
console.log(response);
//将成功之后的回调带过来
this.image_url = response.httpPath
console.log(file);
console.log(fileList);
},
uploadFile() {
// 触发提交的操作
console.log('触发-----------uploadFile----------------');
this.$refs.myupload.submit();
},
errorTest(a,b,c){
console.log(a);
console.log(b);
console.log(c);
},
progress(event, file, fileList){
console.log(event);
console.log(file);
console.log(fileList);
}
}
}
</script>
2、提交方式2--使用el的http-request自定义的action的提交
<el-upload ref="myupload"
name="file"
:action="nodeFile" accept="image/png,image/jpg,image/jpeg"
list-type="picture-card"
:limit=2
:auto-upload="false"
:on-exceed="handleExceed"
:before-upload="handleBeforeUpload"
:on-preview="handlePictureCardPreview"
:on-remove="handleRemove"
:on-success="handleAvatarSuccess"
:on-error = 'errorTest'
:on-progress = 'progress'
:http-request = "handlePost"
>
<i class="el-icon-plus"></i>
</el-upload>
<el-button size="small" type="primary" @click="uploadFile">立即上传</el-button>
<script>
import axios from "axios";
export default {
data() {
return {
limitNum: 2,
// nodeFile:'http://127.0.0.1:8080/xx-cms/file/uploadFile',
nodeFile:'http://10.204.5.9:8070/xx-cms/file/uploadFile',
image_url:''
}
},
methods: {
// 上传文件之前的钩子
handleBeforeUpload(file) {
console.log('handleBeforeUpload');
if (!(file.type === 'image/png' || file.type === 'image/jpg' || file.type === 'image/jpeg')) {
this.$notify.warning({
title: '警告',
message: '请上传格式为image/png, image/jpg, image/jpeg的图片'
})
}
let size = file.size / 1024 / 1024 / 10;
if (size > 10) {
this.$notify.warning({
title: '警告',
message: '图片大小必须小于10M'
})
}
},
// 文件超出个数限制时的钩子
handleExceed(files, fileList) {
this.$notify.warning({message: '只允许上传一张图片'})
},
// 文件列表移除文件时的钩子
handleRemove(file, fileList) {
console.log(file, fileList);
},
// 点击文件列表中已上传的文件时的钩子
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url;
this.dialogVisible = true;
},
//自带的上传上传成功的回调方法
handleAvatarSuccess(response, file, fileList) {
console.log("handleAvatarSuccess-------");
console.log(response);
//将成功之后的回调带过来
this.image_url = response.httpPath
console.log(file);
console.log(fileList);
},
uploadFile() {
// 触发提交的操作
console.log('触发-----------uploadFile----------------');
this.$refs.myupload.submit();
},
errorTest(a,b,c){
console.log(a);
console.log(b);
console.log(c);
},
progress(event, file, fileList){
console.log(event);
console.log(file);
console.log(fileList);
},
handlePost(file) {
console.log('handlePost----------------');
console.log(file)
const headers = {
// 添加请求头
headers: {
'Content-Type': 'multipart/form-data'
}
};
const formData = new FormData();
formData.append('file', file.file);
axios.post('http://127.0.0.1:8080/xx-cms/file/uploadFile', formData, headers)
.then(res => {
console.log("提交成功,下面打印参数");
console.log(res);
}).catch(err => {
console.log(err);
});
}
}
}
</script>
3、提交方式3--使用this.$refs去获取
<el-upload ref="myupload"
name="file"
:action="nodeFile" accept="image/png,image/jpg,image/jpeg"
list-type="picture-card"
:limit=2
:auto-upload="false"
:on-exceed="handleExceed"
:before-upload="handleBeforeUpload"
:on-preview="handlePictureCardPreview"
:on-remove="handleRemove"
:on-success="handleAvatarSuccess"
:on-error = 'errorTest'
:on-progress = 'progress'
:http-request = "handlePost"
>
<i class="el-icon-plus"></i>
</el-upload>
<el-button size="small" type="primary" @click="uploadFile">立即上传</el-button>
<script>
import axios from "axios";
export default {
data() {
return {
limitNum: 2,
// nodeFile:'http://127.0.0.1:8080/xx-cms/file/uploadFile',
nodeFile:'http://10.204.5.9:8070/xx-cms/file/uploadFile',
image_url:''
}
},
methods: {
// 上传文件之前的钩子
handleBeforeUpload(file) {
console.log('handleBeforeUpload');
if (!(file.type === 'image/png' || file.type === 'image/jpg' || file.type === 'image/jpeg')) {
this.$notify.warning({
title: '警告',
message: '请上传格式为image/png, image/jpg, image/jpeg的图片'
})
}
let size = file.size / 1024 / 1024 / 10;
if (size > 10) {
this.$notify.warning({
title: '警告',
message: '图片大小必须小于10M'
})
}
},
// 文件超出个数限制时的钩子
handleExceed(files, fileList) {
this.$notify.warning({message: '只允许上传一张图片'})
},
// 文件列表移除文件时的钩子
handleRemove(file, fileList) {
console.log(file, fileList);
},
// 点击文件列表中已上传的文件时的钩子
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url;
this.dialogVisible = true;
},
//自带的上传上传成功的回调方法
handleAvatarSuccess(response, file, fileList) {
console.log("handleAvatarSuccess-------");
console.log(response);
//将成功之后的回调带过来
this.image_url = response.httpPath
console.log(file);
console.log(fileList);
},
uploadFile() {
// 触发提交的操作
console.log('触发-----------uploadFile----------------');
//去掉submit,通过点击事件触发的
// this.$refs.myupload.submit();
console.log('uploadFile-------------');
console.log(this.$refs.myupload)
console.log(this.$refs.myupload.uploadFiles)
console.log(this.$refs.myupload.uploadFiles[0])
const headers = {
// 添加请求头
headers: {
'Content-Type': 'multipart/form-data'
}
};
const formData = new FormData();
formData.append('file', this.$refs.myupload.uploadFiles[0].raw);
formData.append('file1', this.$refs.myupload.uploadFiles[1].raw);
axios.post('http://127.0.0.1:8080/atpdc-cms/file/uploadFile', formData, headers)
.then(res => {
console.log(res);
}).catch(err => {
console.log(err);
});
},
errorTest(a,b,c){
console.log(a);
console.log(b);
console.log(c);
},
progress(event, file, fileList){
console.log(event);
console.log(file);
console.log(fileList);
}
}
}
</script>