欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue使用el-upload實(shí)現(xiàn)文件上傳的實(shí)例代碼

 更新時(shí)間:2024年01月03日 11:20:39   作者:數(shù)據(jù)大魔王  
這篇文章主要為大家詳細(xì)介紹了vue使用el-upload實(shí)現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴們可以參考一下

引言

因?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)的效果

    這篇文章主要給大家介紹了利用vue2如何實(shí)現(xiàn)div contenteditable="true",就是類似于v-model的效果,文中給出了兩種解決的思路,對大家具有一定的參考價(jià)值,有需要的朋友們下面來一起看看吧。
    2017-02-02
  • vue+導(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í)例

    今天小編就為大家分享一篇vue+導(dǎo)航錨點(diǎn)聯(lián)動-滾動監(jiān)聽和點(diǎn)擊平滑滾動跳轉(zhuǎn)實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Vue3實(shí)現(xiàn)常見附件的預(yù)覽功能

    Vue3實(shí)現(xiàn)常見附件的預(yù)覽功能

    最近開發(fā)了一個建筑相關(guān)的移動端項(xiàng)目,其中有各種附件預(yù)覽的功能,本文總結(jié)出了一些Vue常用的文件預(yù)覽方式,希望對大家有一定的幫助
    2025-04-04
  • 關(guān)于Vue如何清除緩存的方法詳解

    關(guān)于Vue如何清除緩存的方法詳解

    緩存清除成為克服瀏覽器緩存中過時(shí)內(nèi)容挑戰(zhàn)的關(guān)鍵技術(shù),術(shù)語“緩存清除”是指故意使靜態(tài)資源失效或進(jìn)行版本控制,迫使瀏覽器在發(fā)生更改時(shí)獲取新資源,本文給大家介紹了Vue如何清除緩存,需要的朋友可以參考下
    2023-12-12
  • Vite和Vue CLI的優(yōu)劣

    Vite和Vue CLI的優(yōu)劣

    這篇文章主要介紹了Vite比Vue CLI快在哪里,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2021-01-01
  • 談?wù)剬ue?Router的理解

    談?wù)剬ue?Router的理解

    vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,適合用于構(gòu)建單頁面應(yīng)用,這篇文章主要介紹了對Vue?Router的理解,需要的朋友可以參考下
    2022-11-11
  • Vue?axios和vue-axios的關(guān)系及使用區(qū)別

    Vue?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-08
  • ant-design-vue中tree增刪改的操作方法

    ant-design-vue中tree增刪改的操作方法

    這篇文章主要介紹了ant-design-vue中tree增刪改的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Vue.nextTick純干貨使用方法詳解

    Vue.nextTick純干貨使用方法詳解

    這篇文章主要為大家介紹了Vue.nextTick使用方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • Vue3+element實(shí)現(xiàn)表格數(shù)據(jù)導(dǎo)出

    Vue3+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

最新評論