Streaming video from a database

I am trying to do an action to show media from a database (ASP.NET MVC4):

var memoryStream = new MemoryStream(mediaContent.File.FileData.Data); return new FileStreamResult(memoryStream, MimeMapping.GetMimeMapping(mediaContent.File.Filename)); 

Images display well, but I have a problem with the video (.avi) when I am going to link mysite/media/4 in Chrome or Firefox, which it displays:

 <embed width="100%" height="100%" name="plugin" src="http://mysite/media/4" type="video/x-msvideo"> 

But the video does not play (as it happens if the link points to a real video file), but if I open this link in IE, it will offer me to download the file, and when I open this file from the player, it works fine.

Answer headers:

 Cache-Control:private, s-maxage=0 Content-Length:808680 Content-Type:video/x-msvideo Date:Wed, 06 Nov 2013 10:03:09 GMT Server:Microsoft-IIS/7.5 X-AspNet-Version:4.0.30319 X-AspNetMvc-Version:4.0 X-MiniProfiler-Ids:["e305dcdb-79be-4452-94d2-a9999ffaa13a","c0c81d12-8b31-425c-a57b-2ad186c958d5","1f7f3c09-a695-49f1-9203-6b5bf44b837a","fb0d637e-5926-4759-ad6f-f7322403e98c","f08c0392-10d6-4477-b2df-be52ab9a1d64","366d6122-15a5-41b4-840a-607fc6931996","11fd2eb7-efce-47a1-96f8-09fbdb0b1fa0","690e67b7-b1fb-46a3-9aa3-ef6207203f55","a51640ad-f31d-4f12-a807-6ea06ba0ee46","38adc052-9c41-4243-97d2-41dbf3b36093","9d255225-c122-44ef-8021-5b6f9d4dd549","2b249ff3-9e37-43c3-b6ab-b78b26c6d6ce","2bec0b1b-4898-4b14-bf12-cc331e27ecfc","49c72e01-c8d4-495f-af7e-8ffd687e94e9","1c87e454-f90d-49f4-9618-8dfe0d9c0329","2152a9a8-54ae-47d8-b98a-83ac32dbdb0c","9cf93254-9552-4834-826e-df7e8a7d8e73","a2d782e2-96ca-4e9c-b612-9782a37a06ca","e10ecc8a-5811-4cca-b566-3f09e1de3f2c","3769bb15-60f9-43c3-ad6c-285f3fb47112","1996c4aa-9f76-4f33-95fa-3f7f5b3e72f4"] X-Powered-By:ASP.NET 

What am I doing wrong? I want to have a link that I can use in <object> to display this video on the page.

Update 1: The answer I get if I type the url for a physical video file does not make any difference:

 HTTP/1.1 304 Not Modified Last-Modified: Tue, 05 Nov 2013 17:07:56 GMT Accept-Ranges: bytes ETag: "5d50369249dace1:0" Server: Microsoft-IIS/7.5 X-Powered-By: ASP.NET Date: Wed, 06 Nov 2013 11:20:32 GMT 

But it works and starts playing video in the browser using the built-in VLC plugin.

Update 2: I tried different implementations to return the video and tried to set the link to src object

 <object id="video-player" class="preview-container" type="video/x-msvideo" src="{link to video}" loop="true" controls="false" autoplay="true"></object> 

So, if the link to the real video, for example, "localhost / media / some_video.avi", then it works fine inside the object and if you are going to directly link.

I checked the behavior for different implementations

1) return File(memoryStream, MimeMapping.GetMimeMapping(mediaContent.File.Filename), mediaContent.File.Filename);

to url: request to download a video file; in object: shows an empty plugin;

2) return new FileContentResult(mediaContent.File.FileData.Data, "application/x-vlc-plugin")

URL: shows an empty plugin; in object: shows an empty plugin;

3) return new FileContentResult(mediaContent.File.FileData.Data, "video/avi")

to url: request to download a video file; in object: shows an empty plugin;

Update 3: I did HttpHandler:

 public void ProcessRequest(HttpContext context) { MediaContent mediaContent; //getting mediaContent context.Response.Clear(); context.Response.Buffer = true; context.Response.AppendHeader("Content-Disposition", "inline; filename=" + mediaContent.File.Filename); context.Response.ContentType = MimeMapping.GetMimeMapping(mediaContent.File.Filename); context.Response.BinaryWrite(mediaContent.File.FileData.Data); context.Response.End(); } 

And it just WORKS. OK. Then I did the action with the same logic as the handler:

 [HttpGet] public void Media(int id) { MediaContent mediaContent; //getting mediaContent Response.Clear(); Response.Buffer = true; Response.AppendHeader("Content-Disposition", "inline; filename=" + mediaContent.File.Filename); Response.ContentType = MimeMapping.GetMimeMapping(mediaContent.File.Filename); Response.BinaryWrite(mediaContent.File.FileData.Data); Response.End(); } 

But this action still does not work with the video, and I started comparing the response headers.

Handler:

 HTTP/1.1 200 OK Cache-Control: private Content-Type: video/x-msvideo Server: Microsoft-IIS/7.5 Content-Disposition: inline; filename=Reebok_App_attract640L.avi X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Wed, 06 Nov 2013 17:02:45 GMT Content-Length: 808680 

Act:

 HTTP/1.1 200 OK Cache-Control: private, s-maxage=0 Content-Type: video/x-msvideo Transfer-Encoding: chunked Server: Microsoft-IIS/7.5 X-AspNetMvc-Version: 4.0 Content-Disposition: inline; filename=Reebok_App_attract640L.avi X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Wed, 06 Nov 2013 17:02:47 GMT 

To get rid of Transfer-Encoding, I added Content-Length to my action:

 Response.AddHeader("Content-Length", mediaContent.File.FileData.Data.Length.ToString()); 

I could not get rid of s-maxage=0 , but now the headers are similar (except for s-maxage=0 , X-AspNetMvc-Version: 4.0 and the order of the headers)

+6
source share
2 answers

Ok, I did my best to reflect your original code, minus the database, what I have below is great for me. VS2012, MVC4, local IIS Express, remote IIS7.5, runs on local IE10, remote IE10, remote IE9. The AVI file I used is a random file that I found in my windows \ winsxs folder. I am going to offer you a problem with the client side (especially IE). Perhaps something like problems with processing cookies ( http://mvolo.com/iis-70-forms-authentication-and-embedded-media-players/ ), IE security zone settings, or something else?

By the way, height = 100% for embedding does not work for me, there should be pixels.

controller

 namespace MvcApplication4.Controllers { public class HomeController : Controller { public ActionResult Index() { return new ViewResult(); } public ActionResult Media(int id) { string fn = Server.MapPath("~/App_Data/boxed-delete.avi"); var memoryStream = new MemoryStream(System.IO.File.ReadAllBytes(fn)); return new FileStreamResult(memoryStream, MimeMapping.GetMimeMapping(System.IO.Path.GetFileName(fn))); } } } 

View

 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <div> <!-- http://localhost:54941/Home/Media/3 --> <embed width="100%" height="500" name="plugin" src="~/Home/Media/3" type="video/x-msvideo"> </div> </body> </html> 
+3
source

You can put the file location stored in the database into an array of byte buffers and read from the array in a piece instead of immediately reading the entire array.

 WebRequest wreq = (HttpWebRequest)WebRequest.Create(url); using (HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse()) using (Stream mystream = wresp.GetResponseStream()) { using (BinaryReader reader = new BinaryReader(mystream)) { int length = Convert.ToInt32(wresp.ContentLength); byte[] buffer = new byte[length]; buffer = reader.ReadBytes(length); Response.Clear(); Response.Buffer = false; Response.ContentType = "video/mp4"; while(true) { int bytesRead = myStream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) break; Response.OutputStream.Write(buffer, 0, bytesRead); } Response.End(); } } 

You can also check the data it gives, http://www.devcurry.com/2013/04/play-videos-in-aspnet-mvc-html5-using.html

thanks

-1
source

Source: https://habr.com/ru/post/957490/


All Articles