Vue+Element UI 實(shí)現(xiàn)視頻上傳功能
一、前言
項(xiàng)目中需要提供一個(gè)視頻介紹,使用戶能夠快速、方便的了解如何使用產(chǎn)品以及注意事項(xiàng)。
前臺(tái)使用Vue+Element UI中的el-upload組件實(shí)現(xiàn)視頻上傳及進(jìn)度條展示,后臺(tái)提供視頻上傳API并返回URL。
二、具體實(shí)現(xiàn)
1、效果圖展示



2、HTML代碼
<div class="album albumvideo">
<div>
<p class="type_title">
<span>視頻介紹</span>
</p>
<div class="pic_img">
<div class="pic_img_box">
<el-upload class="avatar-uploader"
action="上傳地址"
v-bind:data="{FoldPath:'上傳目錄',SecretKey:'安全驗(yàn)證'}"
v-bind:on-progress="uploadVideoProcess"
v-bind:on-success="handleVideoSuccess"
v-bind:before-upload="beforeUploadVideo"
v-bind:show-file-list="false">
<video v-if="videoForm.showVideoPath !='' && !videoFlag"
v-bind:src="videoForm.showVideoPath"
class="avatar video-avatar"
controls="controls">
您的瀏覽器不支持視頻播放
</video>
<i v-else-if="videoForm.showVideoPath =='' && !videoFlag"
class="el-icon-plus avatar-uploader-icon"></i>
<el-progress v-if="videoFlag == true"
type="circle"
v-bind:percentage="videoUploadPercent"
style="margin-top:7px;"></el-progress>
</el-upload>
</div>
</div>
</div>
<p class="Upload_pictures">
<span></span>
<span>最多可以上傳1個(gè)視頻,建議大小50M,推薦格式mp4</span>
</p>
</div>JS代碼
<script>
var vm = new Vue({
el: '#app',
data: {
videoFlag: false,
//是否顯示進(jìn)度條
videoUploadPercent: "",
//進(jìn)度條的進(jìn)度,
isShowUploadVideo: false,
//顯示上傳按鈕
videoForm: {
showVideoPath: ''
}
},
methods: {
//上傳前回調(diào)
beforeUploadVideo(file) {
var fileSize = file.size / 1024 / 1024 < 50;
if (['video/mp4', 'video/ogg', 'video/flv', 'video/avi', 'video/wmv', 'video/rmvb', 'video/mov'].indexOf(file.type) == -1) {
layer.msg("請(qǐng)上傳正確的視頻格式");
return false;
}
if (!fileSize) {
layer.msg("視頻大小不能超過(guò)50MB");
return false;
}
this.isShowUploadVideo = false;
},
//進(jìn)度條
uploadVideoProcess(event, file, fileList) {
this.videoFlag = true;
this.videoUploadPercent = file.percentage.toFixed(0) * 1;
},
//上傳成功回調(diào)
handleVideoSuccess(res, file) {
this.isShowUploadVideo = true;
this.videoFlag = false;
this.videoUploadPercent = 0;
//前臺(tái)上傳地址
//if (file.status == 'success' ) {
// this.videoForm.showVideoPath = file.url;
//} else {
// layer.msg("上傳失敗,請(qǐng)重新上傳");
//}
//后臺(tái)上傳地址
if (res.Code == 0) {
this.videoForm.showVideoPath = res.Data;
} else {
layer.msg(res.Message);
}
}
}
})
</script>4.后臺(tái)代碼
/// <summary>
/// 上傳視頻
/// </summary>
/// <returns></returns>
[HttpPost]
public IHttpActionResult UploadVideo()
{
try
{
var secretKey = HttpContext.Current.Request["SecretKey"];
if (secretKey == null || !_secretKey.Equals(secretKey))
return Ok(new Result(-1, "驗(yàn)證身份失敗"));
var files = HttpContext.Current.Request.Files;
if (files == null || files.Count == 0)
return Ok(new Result(-1, "請(qǐng)選擇視頻"));
var file = files[0];
if (file == null)
return Ok(new Result(-1, "請(qǐng)選擇上傳的視頻"));
//存儲(chǔ)的路徑
var foldPath = HttpContext.Current.Request["FoldPath"];
if (foldPath == null)
foldPath = "/Upload";
foldPath = "/UploadFile" + "/" + foldPath;
if (foldPath.Contains("../"))
foldPath = foldPath.Replace("../", "");
//校驗(yàn)是否有該文件夾
var mapPath = AppDomain.CurrentDomain.BaseDirectory + foldPath;
if (!Directory.Exists(mapPath))
Directory.CreateDirectory(mapPath);
//獲取文件名和文件擴(kuò)展名
var extension = Path.GetExtension(file.FileName);
if (extension == null || !".ogg|.flv|.avi|.wmv|.rmvb|.mov|.mp4".Contains(extension.ToLower()))
return Ok(new Result(-1, "格式錯(cuò)誤"));
string newFileName = Guid.NewGuid() + extension;
string absolutePath = string.Format("{0}/{1}", foldPath, newFileName);
file.SaveAs(AppDomain.CurrentDomain.BaseDirectory + absolutePath);
string fileUrl = string.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority, absolutePath);
return Json(new ResultData(0, "success",fileUrl));
}
catch (Exception e)
{
Logger.Error("UploadVideo is error", GetType(), e);
return Json(new Result(-1, "上傳失敗"));
}
}三、總結(jié)
注意:Web.Config文件配置之限制上傳文件大小和時(shí)間的屬性配置(1KB=1024B1MB=1024KB1GB=1024MB)

在Web.Config文件中配置限制上傳文件大小與時(shí)間字符串時(shí),是在<httpRuntime><httpRuntime/>節(jié)中完成的,需要設(shè)置以下2個(gè)屬性:
maxRequestLength屬性:該限制可用于防止因用戶將大量文件傳遞到該服務(wù)器而導(dǎo)致的拒絕服務(wù)攻擊。指定的大小以KB為單位,默認(rèn)值為4096KB(4MB)。executionTimeout屬性:指定在ASP.NET應(yīng)用程序自動(dòng)關(guān)閉前,允許執(zhí)行請(qǐng)求的最大秒數(shù)。單位為秒,默認(rèn)值為110s。
到此這篇關(guān)于Vue+Element UI 實(shí)現(xiàn)視頻上傳功能的文章就介紹到這了,更多相關(guān)vue視頻上傳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項(xiàng)目適配屏幕分辨率與屏幕的縮放適配詳細(xì)教程
現(xiàn)在很多14寸的筆記本,出廠默認(rèn)就是150%的顯示。導(dǎo)致很多時(shí)候我們的項(xiàng)目,自己開(kāi)發(fā)的時(shí)候都是按照100%比例來(lái)開(kāi)發(fā)的,上線了就會(huì)發(fā)現(xiàn)這個(gè)問(wèn)題,今天就這個(gè)問(wèn)題給出解決方案,感興趣的朋友跟隨小編一起看看吧2022-11-11
vue父子組件傳值以及單向數(shù)據(jù)流問(wèn)題詳解
大家應(yīng)該都知道父組件可以向子組件通過(guò)屬性形式傳遞參數(shù),傳遞的參數(shù)也可以隨時(shí)隨意修改;但子組件不能修改父組件傳遞過(guò)來(lái)的參數(shù),所以下面這篇文章主要給大家介紹了關(guān)于vue父子組件傳值以及單向數(shù)據(jù)流問(wèn)題的相關(guān)資料,需要的朋友可以參考下2021-09-09
vant van-list下拉加載更多onload事件問(wèn)題
這篇文章主要介紹了vant van-list下拉加載更多onload事件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
elementUI中el-upload文件上傳的實(shí)現(xiàn)方法
ElementUI的組件支持多種事件鉤子,如http-request、before-upload和on-change,以實(shí)現(xiàn)自定義文件上傳處理,這篇文章主要介紹了elementUI中el-upload文件上傳的實(shí)現(xiàn)方法,需要的朋友可以參考下2024-11-11
Vue3格式化Volar報(bào)錯(cuò)的原因分析與解決
Volar 與vetur相同,volar是一個(gè)針對(duì)vue的vscode插件,下面這篇文章主要給大家介紹了關(guān)于Vue3格式化Volar報(bào)錯(cuò)的原因分析與解決方法,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06

