c # HTML5 Blob object that implements the media player functionality

Background Code:

public ActionResult Video()
        {
            string filePath = @"D:\download\test.mp4";
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
            if (fileInfo.Exists == true)
            {
                FileStream fs = new FileStream(filePath, FileMode.Open);
                byte[] bytes = new byte[(int)fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();

                //Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileInfo.Name));
                Response.AddHeader("Content-Length", "" + fileInfo.Length.ToString());
                Response.AddHeader("Content-Transfer-Encoding", "binary");
                Response.ContentType = "application/octet-stream";  
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();
            }          
        }

Front Code:

HTML:

       <Video ID = " VIDEO_PLAYER " width = " 660. " height = " 364 " Controls = " Controls " > </ Video> 


         Js: 
            // Create XMLHttpRequest object 
            var XHR = new new XMLHttpRequest ();
             // configuration request mode, and the requested address is synchronized 
            xhr.open ( ' the POST ' , ' Connections Video ' , to true );
             // set the result of the request type to BLOB 
            xhr.responseType = 'blob' ;
             // request succeeds callback 
            xhr.onload = function (E) {
                 IF ( the this .status == 200 is ) { // the request was successful
                     // Get the blob 
                    var blob = the this .response;
                     var Video = document.getElementById ( ' VIDEO_PLAYER ' );
                     // Get address of the blob, and assign a value to the container 
                    var obj_url = window.URL.createObjectURL (blob); 
                    video.src = obj_url;
                     //video.play();
                    setTimeout("revokeUrl('" + obj_url + "')", "2000");
                }
            };
            xhr.send();



       function revokeUrl(url) {
            window.URL.revokeObjectURL(url);
        }

 

Guess you like

Origin www.cnblogs.com/vaevvaev/p/11433228.html