HTML5 视频 - 来自 SQL 文件流的流
HTML5 Video - streaming from SQL File Stream
我正在编写一个 ASP.NET (C#) 网络应用程序,人们应该能够通过 FileStream 从我们的 SQL 服务器加载视频。我正在考虑使用 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 提供的视频,我最终会得到一个字节数组,但我不知道如何从那里处理它。我目前有代码:
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();
我忍不住觉得我只是缺少一些简单的东西......我希望它很简单!我是这个领域的新手,所以如果有任何帮助,我们将不胜感激!
如果您需要我提供更多信息,请告诉我!我也必须对音频做同样的事情,但我希望任何解决方案都能解决这两个问题。
这是我用的
[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() 方法是您 return 从中获取 MemoryStream 的地方。当您的 HTML 加载 HTML5 视频时,将向 src 发送 GET 请求。所以 src 需要指向你的 API 方法。这是假设您正在使用 Asp.Net Web Api。
注意:如果您从某处逐条拉取流,那么您可以将其直接写入 outputStream。这是以一种允许您一次拥有整个流然后逐段流式传输的方式编写的。它效率低下,但这是我项目的要求。如果您不必一次流一点点并且一次拥有整个流(看起来像),那么我宁愿 return 来自您的 Api 方法的整个流。
我认为像 BobbyJ 一样,您需要制作一个外部 url 您的视频控件将从中加载 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>
您可以使用 Web 服务(WCF 或 Web api 或任何您想要的)来代替“http://media.w3.org/2010/05/bunny/movie.ogv”url,url 将 return 你一个文件。
我终于得到了我需要的东西。以下是我这样做的方式 - 感谢上述回复,因为它们确实帮助我指明了正确的方向!因此,视频播放器控件存在于没有来源的 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 中的选定行。
我希望这可以帮助其他希望实施相同的人。
我正在编写一个 ASP.NET (C#) 网络应用程序,人们应该能够通过 FileStream 从我们的 SQL 服务器加载视频。我正在考虑使用 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 提供的视频,我最终会得到一个字节数组,但我不知道如何从那里处理它。我目前有代码:
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();
我忍不住觉得我只是缺少一些简单的东西......我希望它很简单!我是这个领域的新手,所以如果有任何帮助,我们将不胜感激!
如果您需要我提供更多信息,请告诉我!我也必须对音频做同样的事情,但我希望任何解决方案都能解决这两个问题。
这是我用的
[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() 方法是您 return 从中获取 MemoryStream 的地方。当您的 HTML 加载 HTML5 视频时,将向 src 发送 GET 请求。所以 src 需要指向你的 API 方法。这是假设您正在使用 Asp.Net Web Api。
注意:如果您从某处逐条拉取流,那么您可以将其直接写入 outputStream。这是以一种允许您一次拥有整个流然后逐段流式传输的方式编写的。它效率低下,但这是我项目的要求。如果您不必一次流一点点并且一次拥有整个流(看起来像),那么我宁愿 return 来自您的 Api 方法的整个流。
我认为像 BobbyJ 一样,您需要制作一个外部 url 您的视频控件将从中加载 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>
您可以使用 Web 服务(WCF 或 Web api 或任何您想要的)来代替“http://media.w3.org/2010/05/bunny/movie.ogv”url,url 将 return 你一个文件。
我终于得到了我需要的东西。以下是我这样做的方式 - 感谢上述回复,因为它们确实帮助我指明了正确的方向!因此,视频播放器控件存在于没有来源的 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 中的选定行。
我希望这可以帮助其他希望实施相同的人。