久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

vue+springboot+element+vue-resource實(shí)現(xiàn)文件上傳教程

瀏覽:114日期:2022-11-15 16:49:07

vue頁面設(shè)置

<el-upload 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)鍵)。

vue+springboot+element+vue-resource實(shí)現(xiàn)文件上傳教程

imageUrl: ’’,<el-form-item label='封面圖片' :required='true'> <el-upload action='http://xxx.cn/xx/file/uploadImg/' name=’photo’ :show-file-list='false' :on-success='handleAvatarSuccess' :before-upload='beforeAvatarUpload'> <img v-if='imageUrl' :src='http://www.baoyu77737.com/bcjs/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)文件上傳教程就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 长沙市| 桦川县| 泽库县| 桂东县| 晋江市| 岳阳市| 江川县| 武义县| 新乡市| 临沂市| 洪洞县| 仙居县| 额敏县| 勐海县| 临桂县| 吴川市| 奈曼旗| 日土县| 井冈山市| 德清县| 石林| 鄂州市| 门源| 天台县| 汉源县| 长子县| 益阳市| 三原县| 阿拉善右旗| 濮阳县| 个旧市| 玉门市| 灵石县| 阿鲁科尔沁旗| 新化县| 蒙阴县| 通许县| 广饶县| 白城市| 吕梁市| 米脂县|