vue使用el-upload實(shí)現(xiàn)文件上傳的實(shí)例代碼
引言
因?yàn)槲沂莢ue+springboot前后分離,要跨域,就不能用默認(rèn)的action寫請求地址,我用axios時(shí)最困擾的就是怎么拿到那個真實(shí)的文件,然后給傳給后臺。
其實(shí)可以通過自帶的onchanne觸發(fā)方法獲得文件列表,文件信息中那個raw就是真實(shí)的文件。
寫的時(shí)候,剛開始我是直接把el-upload里面的button中加了點(diǎn)擊事件,但是每次文件還沒選,就已經(jīng)向后臺發(fā)出請求了,當(dāng)然傳不過去,于是外面套了個form。
element-ui組件使用可以查看文檔
一、前臺Vue
<template> <el-form> <el-form-item label="姓名" prop="name"> <el-input v-model="name"></el-input> </el-form-item> <el-form-item> <el-upload ref="upfile" style="display: inline" :auto-upload="false" :on-change="handleChange" :file-list="fileList" action="#"> <el-button type="success">選擇文件</el-button> </el-upload> </el-form-item> <el-form-item> <el-button type="success" @click="upload">點(diǎn)擊上傳</el-button> </el-form-item> </el-form> </template> <script> export default { name:'UploadUi', data(){ return{ name:'', fileList:[] } }, methods:{ //通過onchanne觸發(fā)方法獲得文件列表 handleChange(file, fileList) { this.fileList = fileList; console.log(fileList) }, upload(){ let fd = new FormData(); fd.append("name",this.name); this.fileList.forEach(item=>{ //文件信息中raw才是真的文件 fd.append("files",item.raw); //console.log(item.raw) }) this.$axios.post('/uploadUi',fd).then(res=>{ if (res.data.code === 200) { //console.log(res); this.$message('上傳成功') }else{ this.$message('失敗') } }) }, } } </script>
二、后臺Springboot
package com.example.demo.controller; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.List; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.example.demo.entity.ListParam; import com.example.demo.entity.MyUser; import com.example.demo.entity.Result; @RestController @ResponseBody @CrossOrigin @RequestMapping("/api") public class UploadController { @PostMapping("/uploadUi") public Result upFile(@RequestParam("name")String name,@RequestParam("files") MultipartFile[] files ) { System.out.println("開始"); System.out.println(name); if(files != null) { for(MultipartFile file : files) { String fileName = file.getOriginalFilename(); System.out.println(fileName); try{ File mkdir = new File("F:\\app\\file"); if(!mkdir.exists()) { mkdir.mkdirs(); } //定義輸出流,將文件寫入硬盤 FileOutputStream os = new FileOutputStream(mkdir.getPath()+"\\"+fileName); InputStream in = file.getInputStream(); int b = 0; while((b=in.read())!=-1){ //讀取文件 os.write(b); } os.flush(); //關(guān)閉流 in.close(); os.close(); }catch(Exception e) { e.printStackTrace(); return new Result(401,"失敗"); } } return new Result(200,"成功"); }else { return new Result(401,"文件找不到"); } } }
總結(jié)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助
到此這篇關(guān)于vue使用el-upload實(shí)現(xiàn)文件上傳的文章就介紹到這了,更多相關(guān)vue el-upload文件上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2 如何實(shí)現(xiàn)div contenteditable=“true”(類似于v-model)的效果
這篇文章主要給大家介紹了利用vue2如何實(shí)現(xiàn)div contenteditable="true",就是類似于v-model的效果,文中給出了兩種解決的思路,對大家具有一定的參考價(jià)值,有需要的朋友們下面來一起看看吧。2017-02-02vue+導(dǎo)航錨點(diǎn)聯(lián)動-滾動監(jiān)聽和點(diǎn)擊平滑滾動跳轉(zhuǎn)實(shí)例
今天小編就為大家分享一篇vue+導(dǎo)航錨點(diǎn)聯(lián)動-滾動監(jiān)聽和點(diǎn)擊平滑滾動跳轉(zhuǎn)實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11Vue3實(shí)現(xiàn)常見附件的預(yù)覽功能
最近開發(fā)了一個建筑相關(guān)的移動端項(xiàng)目,其中有各種附件預(yù)覽的功能,本文總結(jié)出了一些Vue常用的文件預(yù)覽方式,希望對大家有一定的幫助2025-04-04Vue?axios和vue-axios的關(guān)系及使用區(qū)別
axios是基于promise的HTTP庫,可以使用在瀏覽器和node.js中,它不是vue的第三方插件,vue-axios是axios集成到Vue.js的小包裝器,可以像插件一樣安裝使用:Vue.use(VueAxios, axios),本文給大家介紹Vue?axios和vue-axios關(guān)系,感興趣的朋友一起看看吧2022-08-08Vue3+element實(shí)現(xiàn)表格數(shù)據(jù)導(dǎo)出
這篇文章主要為大家學(xué)習(xí)介紹了Vue3如何利用element實(shí)現(xiàn)表格數(shù)據(jù)導(dǎo)出功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧2023-07-07