我正在编写一个ASP.NET(C#)Web应用程序,人们应该能够通过FileStream从我们的SQL Server加载视频。我正在考虑使用HTML5视频控件,因为它简单。我已经设法让它适用于保存在项目中的mp4文件:
<video controls id="vidPlayer" runat="server" width="420">
<source src="Content/Video/video.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video>
这就像文件在那里一样,但是如果我正在寻找从FileStream提供视频,我最终得到一个Byte数组,我不知道如何处理它。我目前有代码:
SqlConnection con = new SqlConnection(constr);
con.Open();
//Retrieve the FilePath() of the video file
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = con;
sqlCommand.CommandText = "SELECT EvidenceFS FROM [EP].[t_TraineeUploadedEvidence] WHERE ID = 'A5E262F8-6465-41C1-BD34-42EBCB492C87'";
byte[] buffer = (byte[])sqlCommand.ExecuteScalar();
MemoryStream ms = new MemoryStream(buffer);
vidPlayer.Src = ????
//Cleanup
con.Close();
我无法帮助但觉得我只是缺少一些简单的东西......我希望它很简单!我对这个领域比较陌生,所以任何帮助都会受到赞赏!
如果您需要我的更多信息,请告诉我们!我也必须对音频做同样的事情,但我希望任何解决方案都有望解决这两个问题。
答案 0 :(得分:1)
这就是我使用的。
[HttpGet, Route("yourApi")]
public HttpResponseMessage grabMyStream(string fileName)
{
string mimeType = MimeMapping.GetMimeMapping(fileName);
MediaTypeHeaderValue mediaType = MediaTypeHeaderValue.Parse(mimeType);
Stream inputStream = getYourStream();
var response = Request.CreateResponse();
response.Content = new PushStreamContent(
async (outputStream, httpContent, transportContext) =>
{
byte[] buffer = new byte[65536];
int read = 0;
while ((read = inputStream.Read(buffer, 0, buffer.Length)) > 0)
{
await outputStream.WriteAsync(buffer, 0, buffer.Length);
}
}, mediaType);
return response;
}
<video controls id="vidPlayer" runat="server" width="420">
<source src="yourApi?fileName=video.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video>
在传递文件名时使用查询字符串,因为最后有一个扩展名。
编辑:getYourStream()方法是从中返回MemoryStream的地方。当您的HTML加载HTML5视频时,会向src发送GET请求。所以src需要指向你的API方法。假设您使用的是Asp.Net Web Api。
注意:如果你从某个地方逐个拉出一条流,那么你可以直接将它写入outputStream。这是以允许您一次拥有整个流然后逐个流式传输的方式编写的。这是低效的,但那些是我的项目的要求。如果你不需要一次流一点并且你有一次整个流(看起来像),那么我只需要从你的Api方法返回整个流。
答案 1 :(得分:1)
我想像BobbyJ一样,你需要制作一个外部网址,你的视频控件将从中加载mp4文件。 例如 - 这是一个简单的视频控件(从这里 - https://www.w3.org/wiki/HTML/Elements/video)。
<video controls
src="http://media.w3.org/2010/05/bunny/movie.ogv">
Your user agent does not support the HTML5 Video element.
</video>
而不是“http://media.w3.org/2010/05/bunny/movie.ogv”网址,您可以使用网址服务(WCF或网络API或任何您想要的内容)使用可返回文件的网址。
答案 2 :(得分:1)
我设法得到了我最终需要的东西。以下是我这样做的方式 - 对上述回答表示赞赏,因为他们确实帮助我指明了正确的方向!因此,视频播放器控件存在于没有源的aspx文件中:
<video controls id="vidPlayer" runat="server" width="500" style="display:none" >
Your browser does not support HTML5 video.
</video>
以及用于从SQL文件流中检索数据的以下Web处理程序:
public class FileStreamHandler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int id = int.Parse(context.Request.QueryString["id"]);
byte[] bytes;
string contentType;
string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
string name;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand("[EPORTAL].[LearnerLoginGetEvidenceFile]"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@EvidenceID", id);
cmd.Connection = con;
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
bytes = (byte[])sdr["Data"];
contentType = sdr["ContentType"].ToString();
name = sdr["Name"].ToString();
con.Close();
}
}
context.Response.Clear();
context.Response.Buffer = true;
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name);
context.Response.ContentType = contentType;
context.Response.BinaryWrite(bytes);
context.Response.End();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
我正在寻找的记录ID通过查询字符串传递。这只是在后面的代码中调用,具有以下内容:
vidPlayer.Src = "FileStreamHandler.ashx?Id=" + SelectedEvID.ToString();
其中SelectedEvID取自Gridview中的选定行。
我希望这有助于其他任何希望实现相同目标的人。