vue+springboot+element+vue-resource實現(xiàn)文件上傳教程
vue頁面設置
<el-upload class="upload-demo" action="" :before-upload="beforeUpload" //上傳前操作 :before-remove="beforeRemove" //移除錢操作 :multiple="false" //禁止多選 :http-request="myUpload" //文件上傳,重寫文件上傳方法,action的路徑不會起作用 accept=".jar" //限制文件選擇類型 :drag="false" :data="param" //參數(shù) :file-list="fileList">//文件顯示列表 <el-button size="small" type="primary">點擊上傳</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的路徑不會起作用*/ 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一個對象的內容
name:"xxxx.jar" status:"success" uid:123456456
參數(shù)
this.param={ taskId:this.taskId }
springboot設置
1.請求的注解: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());// 有的瀏覽器獲取的是路徑+文件名 // 若是文件名為空,說明此時沒有選擇文件,返回,文件上傳失敗,選擇文件 if (fname.length() < 1) { //此時沒有選擇文件 } .... }
補充知識:elementUI upload圖片文件上傳到指定后端接口解決方法
1. 一般后端提供接口上傳文件都有參數(shù)。如果我們不傳參就會報錯或顯示圖片不存在,上傳失敗。所以我們要參考他的文檔。action 是上傳路徑; ==name== 就是傳參的屬性(關鍵)。
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) }, //驗證圖片格式 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實現(xiàn)文件上傳教程就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- SpringBoot基于Minio實現(xiàn)分片上傳、斷點續(xù)傳的實現(xiàn)
- springboot整合vue2-uploader實現(xiàn)文件分片上傳、秒傳、斷點續(xù)傳功能
- springboot大文件上傳、分片上傳、斷點續(xù)傳、秒傳的實現(xiàn)
- SpringBoot 中大文件(分片上傳)斷點續(xù)傳與極速秒傳功能的實現(xiàn)
- 利用Springboot+vue實現(xiàn)圖片上傳至數(shù)據(jù)庫并顯示的全過程
- springboot+vue實現(xiàn)文件上傳下載
- vue+springboot圖片上傳和顯示的示例代碼
- Vue?+?SpringBoot?實現(xiàn)文件的斷點上傳、秒傳存儲到Minio的操作方法
相關文章
Vue實現(xiàn)子組件向父組件傳遞多個參數(shù)的方法
在Vue框架中,組件間的通信是一個常見的需求,特別是在子組件需要向父組件傳遞多個參數(shù)時,合理的通信方式可以顯著提升代碼的可讀性和可維護性,本文將詳細介紹如何在Vue中實現(xiàn)子組件向父組件傳遞多個參數(shù),需要的朋友可以參考下2024-10-10Vue3組件庫框架搭建example環(huán)境的詳細教程
這篇文章主要介紹了Vue3組件庫框架搭建example環(huán)境的詳細教程,本文便搭建?example?開發(fā)環(huán)境和打包構建,并在example中使用組件庫,需要的朋友可以參考下2022-11-11