2017-02-06 42 views
1

我正在從我的mvc操作方法獲取一個字節數組。我想將它設置爲HTMl視頻標籤的來源。如何做呢。我的代碼如下。有沒有辦法在html5源視頻標籤上使用字節數組。

var sourcePDF = '../../Content/Images/video.webm' 
       if (sourcePDF != undefined && sourcePDF.trim() != "") { 
        var url = ResolvedUrl.replace('ActionName', 'StreamFile').replace('ControllerName', 'File'); 
        $.ajax({ 
         type: "POST", 
         url: url, 
         data: JSON.stringify({ filename: sourcePDF }), 
         contentType: "application/json; charset=utf-8", 
         datatype: "JSON", 
         success: function (response) { 
          debugger; 
         console.log(response) 
          var HealthCareIframeAppend = '<video width="100%" height="inherit" src="' + response + '" autoplay controls ></video>'; 
            $('#HealthCareAttachementi').append(HealthCareIframeAppend); 
           $('#_SwitchFullScreenContentNewTab').switchClass('show', 'hide'); 
         }, 
         error: function (error) { 
          console.log(error); 
         } 
        }); 
+1

你會使用'數據:'URI,但除非你的視頻只有幾KB(不太可能),那麼這將是一個壞主意。是否有理由不能將動作設置爲'

+0

我的問題是,如果我設置src源,它工作正常的Firefox,但不是在鉻becoz鉻具有MP4視頻轉換 –

回答

0

您可以使用base64編碼字符串螞蟻然後將它放在你的src點點裏面,但是這將是糟糕的主意,因爲這將是巨大的。你可以在here(查看本頁源代碼)找到演示

教程如何做,你可以找到here

但在你的情況下,額外的挑戰是如何將字節數組轉換爲Base64字符串,這將耗費時間和內存,如果你仍然想這樣做,你可以從服務器返回的base64字符串

+0

字節數組一個base64字符串容易一些兼容性問題。問題在於,對於視頻文件來說,該字符串將很大,這將顯着減少加載時間,可能會導致無法使用。 – Abion47

+0

當然,這很容易讓小字節數組,通過「挑戰」我的意思是大陣列可能會導致服務表現的問題和調用堆棧大小超過了問題 –

+0

所以如何解決。 C#中有解決方案嗎? –

0

我結束了這樣做,它工作得很好。它可以在Chrome和Firefox中完美工作,您也可以在Chrome中下載該文件。 這裏是我的Javascript

var url= '../../Content/Images/video.webm' 
var sourceVideo = parent.window.ResolvedUrl.replace('ActionName', 'videoStream').replace('ControllerName', 'File') + '?' + $.param({ "filePath": url }); 
$('#HealthCareAttachementi video').remove(); 
$('#HealthCareAttachementi iframe').remove();      
var HealthCareIframeAppend = '<video width="100%" height="inherit" autoplay controls > <source src=' + sourceVideo + ' type="video/mp4" ></video>';       $('#HealthCareAttachementi').append(HealthCareIframeAppend); 
$('#_SwitchFullScreenContentNewTab').switchClass('show', 'hide'); 

這裏是操作方法

 public void videoStream(string filePath) 
    { 
     //The header information 
     HttpContext.Response.AddHeader("Content-Disposition", "attachment; filename=Training.mp4"); 

     var file = new FileInfo(filePath); 
     //Check the file exist, it will be written into the response 
     if (file.Exists) 
     { 
      var stream = file.OpenRead(); 
      var bytesinfile = new byte[stream.Length]; 
      stream.Read(bytesinfile, 0, (int)file.Length); 
      HttpContext.Response.BinaryWrite(bytesinfile); 
     } 

    } 
相關問題