這篇文章主要介紹了vue怎么使用el-upload實現文件上傳功能的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇vue怎么使用el-upload實現文件上傳功能文章都會有所收獲,下面我們一起來看看吧。
upload.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"
: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">點擊上傳</el-button>
</el-form-item>
</el-form>
</template>
<script>
export default {
name:'UploadUi',
data(){
return{
name:'',
fileList:[]
}
},
methods:{
//通過onchanne觸發方法獲得文件列表
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后臺 uploadController.java
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(); //關閉流
in.close();
os.close();
}catch(Exception e) {
e.printStackTrace();
return new Result(401,"失敗");
}
}
return new Result(200,"成功");
}else {
return new Result(401,"文件找不到");
}
}
}關于“vue怎么使用el-upload實現文件上傳功能”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“vue怎么使用el-upload實現文件上傳功能”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。