vue+springboot+element+vue-resource實(shí)現(xiàn)文件上傳教程
vue頁(yè)面設(shè)置
<el-upload
class="upload-demo"
action=""
:before-upload="beforeUpload" //上傳前操作
:before-remove="beforeRemove" //移除錢操作
:multiple="false" //禁止多選
:http-request="myUpload" //文件上傳,重寫(xiě)文件上傳方法,action的路徑不會(huì)起作用
accept=".jar" //限制文件選擇類型
:drag="false"
:data="param" //參數(shù)
:file-list="fileList">//文件顯示列表
<el-button size="small" type="primary">點(diǎn)擊上傳</el-button>
<div slot="tip" class="el-upload__tip">只能上傳jar文件,且不超過(guò)500kb</div><!-- :headers="head"-->
</el-upload><!--:on-preview="handlePreview"-->
/*文件上傳前,判斷文件名是否存在,等其他處理*/
beforeUpload(file){
console.log("文件名",file.name,this.fileList)
for (let i = 0; i <this.fileList.length ; i++) {
if (this.fileList[i].name==file.name) {
this.$message.info("文件已存在");
return false;
}
}
this.file=file;
return true;
},
/*文件移除前,提示是否刪除*/
beforeRemove(file,fileList){//delJar
this.$confirm('此操作將永久刪除該文件, 是否繼續(xù)?', '提示', {
confirmButtonText: '確定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.$http.get('/aaaa/task/del?taskId='+this.taskId+'&name='+file.name).then(function(res) {
......
});
}).catch(() => {
this.getJarList();
return false;
});
},
/*文件上傳,重寫(xiě)文件上傳方法,action的路徑不會(huì)起作用*/
myUpload(file){
let fd = new FormData();
fd.append('file',this.file);//傳文件
fd.append('taskId',this.taskId);//傳其他參數(shù)
// fd.append('filename',file.name);//傳其他參數(shù)
this.$http.post('/aaaa/task/add',fd).then(function(res) {
....
});
},
fileList一個(gè)對(duì)象的內(nèi)容
name:"xxxx.jar" status:"success" uid:123456456
參數(shù)
this.param={
taskId:this.taskId
}
springboot設(shè)置
1.請(qǐng)求的注解:produces = "multipart/form-data;charset=utf-8", method = RequestMethod.POS
@RequestMapping(value = "/add", produces = "multipart/form-data;charset=utf-8", method = RequestMethod.POST)
public String addJar(int taskId, HttpServletRequest request) throws IOException, ServletException {
....
//獲取文件
Part part = request.getPart("file");// input的name值
String dis = part.getHeader("Content-Disposition");
// 獲取文件名--sdsf.jar
String fna = dis.substring(dis.indexOf("filename=") + 10, dis.length() - 1);
String fname = fna.substring(fna.lastIndexOf("\\") + 1, fna.length());// 有的瀏覽器獲取的是路徑+文件名
// 若是文件名為空,說(shuō)明此時(shí)沒(méi)有選擇文件,返回,文件上傳失敗,選擇文件
if (fname.length() < 1) {
//此時(shí)沒(méi)有選擇文件
}
....
}
補(bǔ)充知識(shí):elementUI upload圖片文件上傳到指定后端接口解決方法
1. 一般后端提供接口上傳文件都有參數(shù)。如果我們不傳參就會(huì)報(bào)錯(cuò)或顯示圖片不存在,上傳失敗。所以我們要參考他的文檔。action 是上傳路徑; ==name== 就是傳參的屬性(關(guān)鍵)。

imageUrl: '', <el-form-item label="封面圖片" :required="true"> <el-upload class="avatar-uploader" action="http://xxx.cn/xx/file/uploadImg/" name='photo' :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload"> <img v-if="imageUrl" :src="imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </el-form-item>
handleAvatarSuccess(res, file) {
console.log(res)
console.log(file)
this.imageUrl = URL.createObjectURL(file.raw);
console.log(this.imageUrl)
},
//驗(yàn)證圖片格式
beforeAvatarUpload(file) {
const isJPG = file.type === 'image/jpeg';
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isJPG) {
this.$message.error('上傳頭像圖片只能是 JPG 格式!');
}
if (!isLt2M) {
this.$message.error('上傳頭像圖片大小不能超過(guò) 2MB!');
}
return isJPG && isLt2M;
},
css代碼
/* 圖片上傳css */
.avatar-uploader /deep/.el-upload {
border: 1px dashed #d9d9d9;
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
}
.avatar-uploader .el-upload:hover {
border-color: #409EFF;
}
.avatar-uploader-icon {
font-size: 28px;
color: #8c939d;
width: 100px;
height: 100px;
line-height: 100px;
text-align: center;
}
.avatar {
width: 100px;
height: 100px;
display: block;
}
參考elementUI文檔:https://element.eleme.cn/#/zh-CN/component/upload
以上這篇vue+springboot+element+vue-resource實(shí)現(xiàn)文件上傳教程就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot基于Minio實(shí)現(xiàn)分片上傳、斷點(diǎn)續(xù)傳的實(shí)現(xiàn)
- springboot整合vue2-uploader實(shí)現(xiàn)文件分片上傳、秒傳、斷點(diǎn)續(xù)傳功能
- springboot大文件上傳、分片上傳、斷點(diǎn)續(xù)傳、秒傳的實(shí)現(xiàn)
- SpringBoot 中大文件(分片上傳)斷點(diǎn)續(xù)傳與極速秒傳功能的實(shí)現(xiàn)
- 利用Springboot+vue實(shí)現(xiàn)圖片上傳至數(shù)據(jù)庫(kù)并顯示的全過(guò)程
- springboot+vue實(shí)現(xiàn)文件上傳下載
- vue+springboot圖片上傳和顯示的示例代碼
- Vue?+?SpringBoot?實(shí)現(xiàn)文件的斷點(diǎn)上傳、秒傳存儲(chǔ)到Minio的操作方法
相關(guān)文章
Nuxt3項(xiàng)目中問(wèn)題匯總之刷新頁(yè)面useFetch無(wú)返回解決
Nuxt.js是一個(gè)基于 Vue.js 的服務(wù)端渲染應(yīng)用框架,這篇文章主要給大家介紹了關(guān)于Nuxt3項(xiàng)目中問(wèn)題匯總之刷新頁(yè)面useFetch無(wú)返回解決辦法的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03
Vue3項(xiàng)目中引入ElementUI并使用的示例詳解
ElementUI是一個(gè)強(qiáng)大的PC端UI組件框架,它不依賴于vue,但是卻是當(dāng)前和vue配合做項(xiàng)目開(kāi)發(fā)的一個(gè)比較好的ui框架,本文主要介紹了如何在vue3中引入使用ElementUI,需要的可以參考一下2023-06-06
Vue項(xiàng)目中對(duì)index.html中BASE_URL的配置方式
這篇文章主要介紹了Vue項(xiàng)目中對(duì)index.html中BASE_URL的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Vue3對(duì)比Vue2的優(yōu)點(diǎn)總結(jié)
vue3解決了vue2的一些缺陷與弊端,學(xué)習(xí)新的技術(shù)是很有必要的,本文總結(jié)了一些vue3的優(yōu)點(diǎn),希望各位能盡快轉(zhuǎn)入vue3的使用中2021-06-06
element多選表格中使用Switch開(kāi)關(guān)的實(shí)現(xiàn)
當(dāng)在做后臺(tái)管理系統(tǒng)的時(shí)候,會(huì)用到用戶的狀態(tài)管理這個(gè)功能,本文主要介紹了element多選表格中使用Switch開(kāi)關(guān)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Element-UI控件Tree實(shí)現(xiàn)數(shù)據(jù)樹(shù)形結(jié)構(gòu)的方法
這篇文章主要介紹了Element-UI控件Tree實(shí)現(xiàn)數(shù)據(jù)樹(shù)形結(jié)構(gòu),本期介紹添加、修改等功能也比較簡(jiǎn)單,可以通過(guò)element-ui的$prompt彈框控件來(lái)實(shí)現(xiàn),需要的朋友可以參考下2024-01-01
vue+Element-ui實(shí)現(xiàn)分頁(yè)效果
這篇文章主要為大家詳細(xì)介紹了vue+Element-ui實(shí)現(xiàn)分頁(yè)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11

