I am trying to make an action to display media from database (ASP.NET MVC4):
var memoryStream = new MemoryStream(mediaContent.File.FileData.Data);
return new FileStreamResult(memoryStream, MimeMapping.GetMimeMapping(mediaContent.File.Filename));
Pictures are displaying fine, but I have problem with videos (.avi) when I am going to link mysite/media/4
in Chrome or Firefox it displays:
<embed width="100%" height="100%" name="plugin" src="http://mysite/media/4" type="video/x-msvideo">
But video is not playing (as it happens if link pointing to real video file), but if I am opening this link in IE it prompts me to download file and when I am opening this file from player it works fine.
Response 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 I am doing wrong? I want to have a link which I can use in <object>
in order to display this video on page.
Update 1: The response that I am getting if I type url for physical video file doesn't make any sence:
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 start play video inside browser using intaled VLC player plugin.
Update 2:
I have tried different implementations to return video and tried to put link to src
of object
<object id="video-player" class="preview-container" type="video/x-msvideo" src="{link to video}" loop="true" controls="false" autoplay="true"></object>
So in case if link to real video like "localhost/media/some_video.avi"
then it works fine inside object
and if going to url directly.
I checked behavior for different implementations
1) return File(memoryStream, MimeMapping.GetMimeMapping(mediaContent.File.Filename), mediaContent.File.Filename);
to url: prompts to download video file; in object: shows empty plugin;
2) return new FileContentResult(mediaContent.File.FileData.Data, "application/x-vlc-plugin")
to url: shows empty plugin; in object: shows empty plugin;
3) return new FileContentResult(mediaContent.File.FileData.Data, "video/avi")
to url: prompts to download video file; in object: shows empty plugin;
Update 3: I made a 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 simply WORKS. Ok. Then I made action with same logic as 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 not working with video and I started to compare 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
Action:
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 have added Content-Length to my action:
Response.AddHeader("Content-Length", mediaContent.File.FileData.Data.Length.ToString());
I couldn't get rid of s-maxage=0
, but now headers similar (except s-maxage=0
, X-AspNetMvc-Version: 4.0
and header order)