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("視頻大小不能超過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)文章
實(shí)現(xiàn)一個(gè)簡(jiǎn)單的vue無限加載指令方法
vue 中的自定義指令是對(duì)底層 dom 進(jìn)行操作,下面以實(shí)現(xiàn)滾動(dòng)到底部加載數(shù)據(jù),實(shí)現(xiàn)無限加載來介紹如何自定義一個(gè)簡(jiǎn)單的指令。2017-01-01
基于vue循環(huán)列表時(shí)點(diǎn)擊跳轉(zhuǎn)頁面的方法
今天小編就為大家分享一篇基于vue循環(huán)列表時(shí)點(diǎn)擊跳轉(zhuǎn)頁面的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
vue項(xiàng)目中仿element-ui彈框效果的實(shí)例代碼
這篇文章主要介紹了vue項(xiàng)目中仿element-ui彈框效果的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
Vue實(shí)現(xiàn)監(jiān)聽某個(gè)元素滾動(dòng),親測(cè)有效
這篇文章主要介紹了Vue實(shí)現(xiàn)監(jiān)聽某個(gè)元素滾動(dòng),親測(cè)有效!具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Vue.extend 編程式插入組件的實(shí)現(xiàn)
這篇文章主要介紹了Vue.extend 編程式插入組件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

