国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Element-UI的Upload 上傳文件

這篇具有很好參考價(jià)值的文章主要介紹了Element-UI的Upload 上傳文件。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

1、Element-UI版本

"element-ui": "^2.15.9"

Upload 上傳官方文檔

2、一次只能上傳一個(gè)文件

2.1 自動(dòng)上傳

限制一次只能上傳一個(gè)文件,并判斷上傳的文件大小及文件類型;

自動(dòng)上傳:即選擇文件后即開始校驗(yàn),校驗(yàn)通過(guò)后調(diào)接口上傳到服務(wù)器

<template>
    <div class="upload-content">
        <el-upload
            ref="uploadFile"
            drag
            action="sys/file/upload"
            :multiple="false"
            :limit="1"
            accept=".txt, .zip, .rar"
            :before-upload="beforeUpload"
            :on-success="uploadSuccess"
            :on-error="uploadError"
            :on-exceed="uploadExceed">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">將文件拖到此處,或<em>點(diǎn)擊上傳</em></div>
            <div class="el-upload__tip" slot="tip">只能上傳txt/zip/rar文件,且不超過(guò)10M,一次只能上傳一個(gè)</div>
        </el-upload>
    </div>
</template>
<script>
export default {
    name: 'upload-file',
    data() {
        return {};
    },
    methods: {
        // 文件上傳前對(duì)文件類型、文件大小判斷限制
        beforeUpload(file) {
            const { name, size } = file;
            const index = name.lastIndexOf('.');
            // 判斷文件名是否有后綴,沒(méi)后綴文件錯(cuò)誤
            if(index === -1) {
                this.$notify.error({
                    title: '錯(cuò)誤',
                    message: '文件錯(cuò)誤,請(qǐng)重新上傳!',
                });
                return false;
            }
            const fileType = name.substr(index + 1);
            const acceptFileTypes = ['txt', 'zip', 'rar'];
            // 判斷文件類型
            if(!acceptFileTypes.includes(fileType)) {
                this.$notify.error({
                    title: '錯(cuò)誤',
                    message: '文件類型錯(cuò)誤,請(qǐng)重新上傳!',
                });
                return false;
            }
            // 判斷文件大小
            if(size > 10*1024*1024) {
                this.$notify.error({
                    title: '錯(cuò)誤',
                    message: '文件大小超過(guò)10M,請(qǐng)重新上傳!',
                });
                return false;
            }
            // 默認(rèn)true
            return true;
        },

        // 上傳接口調(diào)取成功status為200
        uploadSuccess(res) {
            if(res.code === 200) {  // 文件上傳成功
                this.$notify.success({
                    title:'成功',
                    message: '文件上傳成功!',
                });
            } else {
                this.uploadError();
            }
        },

        // 文件上傳失敗
        uploadError() {
            this.$notify.error({
                title: '錯(cuò)誤',
                message: '文件上傳失敗!',
            });
        },

        // 文件個(gè)數(shù)超過(guò)限制
        uploadExceed() {
            this.$notify.warning({
                title:'提示',
                message: '您已添加了一個(gè)文件,如需替換,請(qǐng)先刪除已添加的文件!',
            });
        },
    }
}
</script>
<style scoped>
.upload-content {
    margin: 40px auto;
    width: 400px;
    text-align: center;
}
</style>

Element-UI的Upload 上傳文件

Element-UI的Upload 上傳文件

2.2 手動(dòng)上傳

限制一次只能上傳一個(gè)文件,并判斷上傳的文件大小及文件類型;

手動(dòng)上傳:設(shè)置auto-upload為false。選擇文件后等待觸發(fā)上傳的動(dòng)作,比如點(diǎn)擊按鈕觸發(fā)上傳文件,會(huì)先走before-upload校驗(yàn)文件,校驗(yàn)通過(guò)后調(diào)接口上傳到服務(wù)器。

<template>
    <div class="upload-content">
        <el-upload
            ref="uploadFile"
            drag
            action="sys/file/upload"
            :multiple="false"
            :limit="1"
            accept=".txt, .zip, .rar"
            :auto-upload="false"
            :before-upload="beforeUpload"
            :on-success="uploadSuccess"
            :on-error="uploadError"
            :on-exceed="uploadExceed">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">將文件拖到此處,或<em>點(diǎn)擊上傳</em></div>
            <div class="el-upload__tip" slot="tip">只能上傳txt/zip/rar文件,且不超過(guò)10M,一次只能上傳一個(gè)</div>
        </el-upload>
        <div class="btn-footer">
            <el-button size="small" type="primary" @click="submit">確 定</el-button>
        </div>
    </div>
</template>
<script>
export default {
    name: 'upload-file',
    data() {
        return {};
    },
    methods: {
        // 點(diǎn)擊按鈕手動(dòng)上傳,會(huì)先觸發(fā)beforeUpload,再執(zhí)行上傳
        submit() {
            this.$refs.uploadFile.submit();
        },

        // 文件上傳前對(duì)文件類型、文件大小判斷限制
        beforeUpload(file) {
            const { name, size } = file;
            const index = name.lastIndexOf('.');
            // 判斷文件名是否有后綴,沒(méi)后綴文件錯(cuò)誤
            if(index === -1) {
                this.$notify.error({
                    title: '錯(cuò)誤',
                    message: '文件錯(cuò)誤,請(qǐng)重新上傳!',
                });
                return false;
            }
            const fileType = name.substr(index + 1);
            const acceptFileTypes = ['txt', 'zip', 'rar'];
            // 判斷文件類型
            if(!acceptFileTypes.includes(fileType)) {
                this.$notify.error({
                    title: '錯(cuò)誤',
                    message: '文件類型錯(cuò)誤,請(qǐng)重新上傳!',
                });
                return false;
            }
            // 判斷文件大小
            if(size > 10*1024*1024) {
                this.$notify.error({
                    title: '錯(cuò)誤',
                    message: '文件大小超過(guò)10M,請(qǐng)重新上傳!',
                });
                return false;
            }
            // 默認(rèn)true
            return true;
        },

        // 上傳接口調(diào)取成功status為200
        uploadSuccess(res) {
            if(res.code === 200) {  // 文件上傳成功
                this.$notify.success({
                    title:'成功',
                    message: '文件上傳成功!',
                });
            } else {
                this.uploadError();
            }
        },

        // 文件上傳失敗
        uploadError() {
            this.$notify.error({
                title: '錯(cuò)誤',
                message: '文件上傳失?。?,
            });
        },

        // 文件個(gè)數(shù)超過(guò)限制
        uploadExceed() {
            this.$notify.warning({
                title:'提示',
                message: '您已添加了一個(gè)文件,如需替換,請(qǐng)先刪除已添加的文件!',
            });
        },
    }
}
</script>
<style scoped>
.upload-content {
    margin: 40px auto;
    width: 400px;
    text-align: center;
}
.btn-footer {
    margin-top: 10px;
}
</style>

Element-UI的Upload 上傳文件

?3、一次可上傳多個(gè)文件

3.1 自動(dòng)上傳

<template>
    <div class="upload-content">
        <el-upload
            ref="uploadFile"
            drag
            action="sys/file/upload"
            multiple
            :limit="5"
            accept=".txt, .zip, .rar"
            :before-upload="beforeUpload"
            :on-success="uploadSuccess"
            :on-error="uploadError"
            :on-exceed="uploadExceed">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">將文件拖到此處,或<em>點(diǎn)擊上傳</em></div>
            <div class="el-upload__tip" slot="tip">只能上傳txt/zip/rar文件,且不超過(guò)10M</div>
        </el-upload>
    </div>
</template>
<script>
export default {
    name: 'upload-file',
    data() {
        return {};
    },
    methods: {
        // 文件上傳前對(duì)文件類型、文件大小判斷限制
        beforeUpload(file) {
            const { name, size } = file;
            const index = name.lastIndexOf('.');
            // 判斷文件名是否有后綴,沒(méi)后綴文件錯(cuò)誤
            if(index === -1) {
                this.$notify.error({
                    title: '錯(cuò)誤',
                    message: `${name}文件錯(cuò)誤,請(qǐng)重新上傳!`,
                });
                return false;
            }
            const fileType = name.substr(index + 1);
            const acceptFileTypes = ['txt', 'zip', 'rar'];
            // 判斷文件類型
            if(!acceptFileTypes.includes(fileType)) {
                this.$notify.error({
                    title: '錯(cuò)誤',
                    message: `${name}文件類型錯(cuò)誤,請(qǐng)重新上傳!`,
                });
                return false;
            }
            // 判斷文件大小
            if(size > 10*1024*1024) {
                this.$notify.error({
                    title: '錯(cuò)誤',
                    message: `${name}文件大小超過(guò)10M,請(qǐng)重新上傳!`,
                });
                return false;
            }
            // 默認(rèn)true
            return true;
        },

        // 上傳接口調(diào)取成功status為200
        uploadSuccess(res, file) {
            if(res.code === 200) {  // 文件上傳成功
                this.$notify.success({
                    title:'成功',
                    message: `${file.name}文件上傳成功!`,
                });
            } else {
                this.uploadError();
            }
        },

        // 文件上傳失敗
        uploadError() {
            this.$notify.error({
                title: '錯(cuò)誤',
                message: '文件上傳失??!',
            });
        },

        // 文件個(gè)數(shù)超過(guò)限制
        uploadExceed(files, fileList) {
            this.$notify.warning({
                title:'提示',
                message: `當(dāng)前限制一次可選擇 5 個(gè)文件,本次選擇了 ${files.length} 個(gè)文件,共選擇了 ${files.length + fileList.length} 個(gè)文件`,
            });
        },
    }
}
</script>
<style scoped>
.upload-content {
    margin: 40px auto;
    width: 400px;
    text-align: center;
}
</style>

Element-UI的Upload 上傳文件

Element-UI的Upload 上傳文件

3.2 手動(dòng)上傳

<template>
    <div class="upload-content">
        <el-upload
            ref="uploadFile"
            drag
            action="sys/file/upload"
            multiple
            :limit="5"
            accept=".txt, .zip, .rar"
            :auto-upload="false"
            :before-upload="beforeUpload"
            :on-success="uploadSuccess"
            :on-error="uploadError"
            :on-exceed="uploadExceed">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">將文件拖到此處,或<em>點(diǎn)擊上傳</em></div>
            <div class="el-upload__tip" slot="tip">只能上傳txt/zip/rar文件,且不超過(guò)10M</div>
        </el-upload>
        <div class="btn-footer">
            <el-button size="small" type="primary" @click="submit">確 定</el-button>
        </div>
    </div>
</template>
<script>
export default {
    name: 'upload-file',
    data() {
        return {};
    },
    methods: {
        // 點(diǎn)擊按鈕手動(dòng)上傳,會(huì)先觸發(fā)beforeUpload,再執(zhí)行上傳
        submit() {
            this.$refs.uploadFile.submit();
        },

        // 文件上傳前對(duì)文件類型、文件大小判斷限制
        beforeUpload(file) {
            const { name, size } = file;
            const index = name.lastIndexOf('.');
            // 判斷文件名是否有后綴,沒(méi)后綴文件錯(cuò)誤
            if(index === -1) {
                this.$notify.error({
                    title: '錯(cuò)誤',
                    message: `${name}文件錯(cuò)誤,請(qǐng)重新上傳!`,
                });
                return false;
            }
            const fileType = name.substr(index + 1);
            const acceptFileTypes = ['txt', 'zip', 'rar'];
            // 判斷文件類型
            if(!acceptFileTypes.includes(fileType)) {
                this.$notify.error({
                    title: '錯(cuò)誤',
                    message: `${name}文件類型錯(cuò)誤,請(qǐng)重新上傳!`,
                });
                return false;
            }
            // 判斷文件大小
            if(size > 10*1024*1024) {
                this.$notify.error({
                    title: '錯(cuò)誤',
                    message: `${name}文件大小超過(guò)10M,請(qǐng)重新上傳!`,
                });
                return false;
            }
            // 默認(rèn)true
            return true;
        },

        // 上傳接口調(diào)取成功status為200
        uploadSuccess(res, file) {
            if(res.code === 200) {  // 文件上傳成功
                this.$notify.success({
                    title:'成功',
                    message: `${file.name}文件上傳成功!`,
                });
            } else {
                this.uploadError();
            }
        },

        // 文件上傳失敗
        uploadError() {
            this.$notify.error({
                title: '錯(cuò)誤',
                message: '文件上傳失敗!',
            });
        },

        // 文件個(gè)數(shù)超過(guò)限制
        uploadExceed(files, fileList) {
            this.$notify.warning({
                title:'提示',
                message: `當(dāng)前限制一次可選擇 5 個(gè)文件,本次選擇了 ${files.length} 個(gè)文件,共選擇了 ${files.length + fileList.length} 個(gè)文件`,
            });
        },
    }
}
</script>
<style scoped>
.upload-content {
    margin: 40px auto;
    width: 400px;
    text-align: center;
}
.btn-footer {
    margin-top: 10px;
}
</style>

Element-UI的Upload 上傳文件

Element-UI的Upload 上傳文件

?4、隱藏提示 “按 delete 鍵可刪除”

Element-UI的Upload 上傳文件

設(shè)置樣式即可文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-503844.html

.upload-content /deep/ .el-upload-list__item  .el-icon-close-tip {
  display: none !important;
}

到了這里,關(guān)于Element-UI的Upload 上傳文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 使用element-ui的el-upload進(jìn)行excel文件上傳與下載

    使用element-ui的el-upload進(jìn)行excel文件上傳與下載

    需求:前端上傳文件后,后端接受文件進(jìn)行處理后直接返回處理后的文件,前端直接再將文件下載下來(lái)。 通常我們都是直接使用el-upload的action屬性來(lái)直接完成文件上傳的操作,如果要進(jìn)行后續(xù)文件的直接下載,可以在on-success中用代碼進(jìn)行操作,若存在401權(quán)限問(wèn)題也可以直接

    2024年02月11日
    瀏覽(37)
  • 擴(kuò)展element-ui el-upload組件,實(shí)現(xiàn)復(fù)制粘貼上傳圖片文件,帶圖片預(yù)覽功能

    擴(kuò)展element-ui el-upload組件,實(shí)現(xiàn)復(fù)制粘貼上傳圖片文件,帶圖片預(yù)覽功能

    控件改造 在窗口的 el-form 控件參數(shù)中添加 @paste.native 事件,事件綁定方法名 handlePaste 也可以在其他控件中添加事件監(jiān)聽,看個(gè)人需求。 注意: 監(jiān)聽粘貼事件時(shí),需要當(dāng)前頁(yè)面先獲取焦點(diǎn),否則無(wú)法正常監(jiān)聽, 可以在頁(yè)面加載后調(diào)用 focus() 獲取焦點(diǎn) 粘貼功能Js部分參考資料

    2023年04月08日
    瀏覽(124)
  • element-ui的el-upload上傳文件按鈕在選取文件按鈕禁用后仍可點(diǎn)擊問(wèn)題

    element-ui的el-upload上傳文件按鈕在選取文件按鈕禁用后仍可點(diǎn)擊問(wèn)題

    element-ui的el-upload上傳文件按鈕在選取文件按鈕禁用后仍可點(diǎn)擊,如下圖: 點(diǎn)擊按鈕已經(jīng)置灰,但是仍能點(diǎn)開選擇圖片彈窗,雖然無(wú)法上傳,但是體驗(yàn)不好。 聽說(shuō)是因?yàn)椋篸isabled只是禁用了點(diǎn)擊事件,并沒(méi)有禁用打開文件選擇窗口 方法一: 附代碼: 方法二: 換成一個(gè)假的

    2024年02月11日
    瀏覽(63)
  • Element-UI 多個(gè)el-upload組件自定義上傳,不用上傳url,并且攜帶自定義傳參(文件序號(hào))
  • element-ui upload圖片上傳組件使用

    element-ui upload圖片上傳組件使用

    圖片上傳前端收集 數(shù)據(jù) 再調(diào)用接口發(fā)送到后端 組件標(biāo)簽內(nèi)的參數(shù): 參數(shù) 說(shuō)明 類型 可選值 默認(rèn)值 action 必選參數(shù),上傳的地址 string — — headers 設(shè)置上傳的請(qǐng)求頭部 object — — multiple 是否支持多選文件 boolean — — data 上傳時(shí)附帶的額外參數(shù) object — — name 上傳的文件字段

    2023年04月19日
    瀏覽(26)
  • 【element-UI】el-upload本地上傳圖片,點(diǎn)擊表單提交和一起上傳,使用formdata對(duì)象上傳

    【element-UI】el-upload本地上傳圖片,點(diǎn)擊表單提交和一起上傳,使用formdata對(duì)象上傳

    需求效果如下圖: 因?yàn)楹蠖艘髨D片需要和其他參數(shù)一起提交,使用formdata對(duì)象攜帶參數(shù),通過(guò)設(shè)置el-upload中action參數(shù)值為#,以及auto-upload(是否在選取文件后立即進(jìn)行上傳)為false 接口api

    2024年02月12日
    瀏覽(96)
  • vue前端直接使用element-ui的upload組件上傳到阿里云OSS存儲(chǔ)

    vue前端直接使用element-ui的upload組件上傳到阿里云OSS存儲(chǔ)

    因?yàn)楣镜姆?wù)器比較拉吧,所以老大決定將數(shù)據(jù)文件等上傳到阿里云服務(wù)器,通過(guò)ali-oss方式,這樣的話讀取文件也比較快? (能看到這說(shuō)明什么安裝element-ui都會(huì)了,我就不詳細(xì)介紹了,直接跳過(guò))? 在執(zhí)行下面操作之前,你要確定你的阿里云能看到上邊這個(gè)頁(yè)面? ?這里主

    2024年02月10日
    瀏覽(37)
  • element-ui upload 上傳組件中on-success 聲明方法不生效問(wèn)題

    最近在學(xué)習(xí)vue 2,實(shí)現(xiàn)element-ui 框架中upload 上傳文件組件碰到的一些坑: 1.上傳文件成功后on-success 聲明的方法不執(zhí)行。 官方模板是上面代碼,但是我是實(shí)現(xiàn)自定義上傳文件,禁用了action實(shí)現(xiàn)方式,添加 :auto-upload=“false” ,然后實(shí)現(xiàn)**:http-request=“submitUpload”**,具體的調(diào)用上

    2024年02月11日
    瀏覽(24)
  • 解決element-ui中組件【el-upload】一次性上傳多張圖片的問(wèn)題

    前端 后端 后端使用的是multer中間件,用來(lái)接收前端發(fā)送過(guò)來(lái)的multipart/form-data形式的數(shù)據(jù) multer.js router.js

    2024年02月12日
    瀏覽(30)
  • 【element-ui組件】一.圖像組件;二.導(dǎo)航菜單(NavMenu);三.上傳組件:Upload;四.分頁(yè)組件

    目錄 一.圖像組件 二.導(dǎo)航菜單(NavMenu) 1.導(dǎo)航方向:通過(guò)model屬性來(lái)設(shè)置 2.菜單項(xiàng): 三.上傳組件:Upload 四.分頁(yè)組件 1.實(shí)現(xiàn)分頁(yè)的方式: (1)服務(wù)器端分頁(yè):通過(guò)底層的數(shù)據(jù)庫(kù)來(lái)實(shí)現(xiàn),前端會(huì)頻繁的和服務(wù)器交互,客戶端每次請(qǐng)求的是分頁(yè)的數(shù)據(jù)而不是所有數(shù)據(jù)

    2023年04月24日
    瀏覽(22)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包