vue使用el-upload實(shí)現(xiàn)文件上傳功能
因?yàn)槲沂莢ue+springboot前后分離,要跨域,就不能用默認(rèn)的action寫(xiě)請(qǐng)求地址,我用axios時(shí)最困擾的就是怎么拿到那個(gè)真實(shí)的文件,然后給傳給后臺(tái)。
其實(shí)可以通過(guò)自帶的onchanne觸發(fā)方法獲得文件列表,文件信息中那個(gè)raw就是真實(shí)的文件。
寫(xiě)的時(shí)候,剛開(kāi)始我是直接把el-upload里面的button中加了點(diǎn)擊事件,但是每次文件還沒(méi)選,就已經(jīng)向后臺(tái)發(fā)出請(qǐng)求了,當(dāng)然傳不過(guò)去,于是外面套了個(gè)form。
element-ui組件使用可以查看文檔
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"
? ? ? ? ? ? ? ? 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:{
? ? ? ? //通過(guò)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后臺(tái) 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("開(kāi)始");
?? ??? ? 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();
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?//定義輸出流,將文件寫(xiě)入硬盤(pán)
?? ??? ??? ??? ??? ? 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,"文件找不到");
?? ??? ? }
?? ??? ?
?? ?}
?? ?
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)背景圖片鋪滿(mǎn)整個(gè)屏幕(適配所有機(jī)型)
在網(wǎng)頁(yè)設(shè)計(jì)中,背景全屏是一種常見(jiàn)的視覺(jué)效果,通過(guò)正確的CSS樣式設(shè)置,可以實(shí)現(xiàn)背景全屏且內(nèi)容在固定一屏大小內(nèi)完全顯示,如果內(nèi)容超出一屏,則可以通過(guò)滾動(dòng)條查看剩余內(nèi)容,這種設(shè)計(jì)可以提升用戶(hù)的瀏覽體驗(yàn),使網(wǎng)頁(yè)看起來(lái)更加整潔和專(zhuān)業(yè)2024-10-10
多個(gè)Vue項(xiàng)目部署到服務(wù)器的步驟記錄
這篇文章主要給大家介紹了關(guān)于多個(gè)Vue項(xiàng)目部署到服務(wù)器的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
vue組件實(shí)現(xiàn)列表自動(dòng)無(wú)限循環(huán)的方法
最近剛好有個(gè)功能需要實(shí)現(xiàn)列表的無(wú)限循環(huán)滾動(dòng),這篇文章主要給大家介紹了關(guān)于vue組件實(shí)現(xiàn)列表自動(dòng)無(wú)限循環(huán)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
vue使用ArcGis?API?for?js創(chuàng)建地圖實(shí)現(xiàn)示例
這篇文章主要為大家介紹了vue使用ArcGis?API?for?js創(chuàng)建地圖實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
vue實(shí)現(xiàn)列表無(wú)縫循環(huán)滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)列表無(wú)縫循環(huán)滾動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
解決Vue中的生命周期beforeDestory不觸發(fā)的問(wèn)題
這篇文章主要介紹了解決Vue中的生命周期beforeDestory不觸發(fā)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
el-select 點(diǎn)擊按鈕滾動(dòng)到選擇框頂部的實(shí)現(xiàn)代碼
本文通過(guò)實(shí)例代碼給大家分享el-select 點(diǎn)擊按鈕滾動(dòng)到選擇框頂部效果,主要代碼是在visibleChange在這個(gè)popper里面找到.el-select-dropdown__list,感興趣的朋友跟隨小編一起看看吧2024-05-05
vue axios 在頁(yè)面切換時(shí)中斷請(qǐng)求方法 ajax
下面小編就為大家分享一篇vue axios 在頁(yè)面切換時(shí)中斷請(qǐng)求方法 ajax,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
一文詳解Vue3的watch是如何實(shí)現(xiàn)監(jiān)聽(tīng)的
watch這個(gè)API大家都很熟悉,今天這篇文章小編來(lái)帶你搞清楚Vue3的watch是如何實(shí)現(xiàn)對(duì)響應(yīng)式數(shù)據(jù)進(jìn)行監(jiān)聽(tīng)的,希望對(duì)大家有一定的幫助2024-11-11

