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" //文件上傳,重寫文件上傳方法,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文件,且不超過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; }); }, /*文件上傳,重寫文件上傳方法,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());// 有的瀏覽器獲取的是路徑+文件名 // 若是文件名為空,說明此時(shí)沒有選擇文件,返回,文件上傳失敗,選擇文件 if (fname.length() < 1) { //此時(shí)沒有選擇文件 } .... }
補(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('上傳頭像圖片大小不能超過 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ù)并顯示的全過程
- springboot+vue實(shí)現(xiàn)文件上傳下載
- vue+springboot圖片上傳和顯示的示例代碼
- Vue?+?SpringBoot?實(shí)現(xiàn)文件的斷點(diǎn)上傳、秒傳存儲(chǔ)到Minio的操作方法
相關(guān)文章
掌握Qwik.js,輕松應(yīng)對(duì)前端SSR渲染問題
掌握Qwik.js,輕松應(yīng)對(duì)前端SSR渲染問題!本指南將為你揭示如何使用這款強(qiáng)大的框架,讓你在前端開發(fā)中游刃有余2023-12-12Vue實(shí)現(xiàn)子組件向父組件傳遞多個(gè)參數(shù)的方法
在Vue框架中,組件間的通信是一個(gè)常見的需求,特別是在子組件需要向父組件傳遞多個(gè)參數(shù)時(shí),合理的通信方式可以顯著提升代碼的可讀性和可維護(hù)性,本文將詳細(xì)介紹如何在Vue中實(shí)現(xiàn)子組件向父組件傳遞多個(gè)參數(shù),需要的朋友可以參考下2024-10-10Vue3組件庫(kù)框架搭建example環(huán)境的詳細(xì)教程
這篇文章主要介紹了Vue3組件庫(kù)框架搭建example環(huán)境的詳細(xì)教程,本文便搭建?example?開發(fā)環(huán)境和打包構(gòu)建,并在example中使用組件庫(kù),需要的朋友可以參考下2022-11-11vue使用highcharts自定義圖例點(diǎn)擊事件
這篇文章主要為大家詳細(xì)介紹了vue使用highcharts自定義圖例點(diǎn)擊事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03