vue+springboot+element+vue-resource實(shí)現(xiàn)文件上傳教程
vue頁面設(shè)置
<el-upload 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'>點(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的路徑不會起作用*/ 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一個對象的內(nèi)容
name:'xxxx.jar'status:'success'uid:123456456
參數(shù)
this.param={ taskId:this.taskId}
springboot設(shè)置
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) { //此時沒有選擇文件 } .... }
補(bǔ)充知識:elementUI upload圖片文件上傳到指定后端接口解決方法
1. 一般后端提供接口上傳文件都有參數(shù)。如果我們不傳參就會報(bào)錯或顯示圖片不存在,上傳失敗。所以我們要參考他的文檔。action 是上傳路徑; ==name== 就是傳參的屬性(關(guā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.aoyou183.cn/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)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. XML入門的常見問題(一)2. Jsp中request的3個基礎(chǔ)實(shí)踐3. 怎樣才能用js生成xmldom對象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?4. IntelliJ IDEA 統(tǒng)一設(shè)置編碼為utf-8編碼的實(shí)現(xiàn)5. Django ORM實(shí)現(xiàn)按天獲取數(shù)據(jù)去重求和例子6. jsp EL表達(dá)式詳解7. idea給項(xiàng)目打war包的方法步驟8. chat.asp聊天程序的編寫方法9. idea修改背景顏色樣式的方法10. idea設(shè)置自動導(dǎo)入依賴的方法步驟
