6

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)

4

2 回答 2

2

好吧,我尽力镜像您的初始代码,减去数据库,我下面的内容对我来说很好。VS2012,MVC4,IIS Express 本地,IIS7.5 远程,适用于 IE10 本地,IE10 远程,IE9 远程。我使用的 AVI 文件是我在 windows\winsxs 文件夹中找到的一些随机文件。我将建议您遇到客户端问题(特别是 IE)。可能是 cookie 处理问题(http://mvolo.com/iis-70-forms-authentication-and-embedded-media-players/)、IE安全区域设置或其他问题?

顺便说一句,嵌入时 height=100% 对我不起作用,需要像素。

控制器

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)));
        }

    }
}

看法

@{
    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>
于 2013-11-10T22:28:05.750 回答
-1

您可以将存储在数据库中的文件位置放入字节缓冲区数组中,并以块的形式从数组中读取,而不是一次读取整个数组

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();
  }
}

您还可以查看它有帮助的给定 url 天气, http://www.devcurry.com/2013/04/play-videos-in-aspnet-mvc-html5-using.html

谢谢

于 2013-11-09T17:23:18.150 回答