【发布时间】:2014-08-05 21:34:46
【问题描述】:
我有一个 Web 表单应用程序,它具有保存来自 IP 摄像机的图像的功能。我添加了一个应该刷新图像的查看器页面,但它不起作用。我想可能是因为该函数在 page_load 中,所以它只在页面第一次加载时保存了新图像。我添加了一个计时器,以便它每 5 秒运行一次保存新图像的功能,但计时器似乎不起作用。代码如下:
namespace PlayVideo
{
public partial class Video : System.Web.UI.Page
{
FileStream fs = File.Open(@"Location of file", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
protected void Page_Load(object sender, EventArgs e)
{
//This is where I originally had the function that saves the new image.
//string saveTo = @"location to save new image";
//FileStream writeStream = new FileStream(saveTo, FileMode.Create, FileAccess.ReadWrite);
ReadWriteStream(fs, writeStream);
Response.Clear();
Response.TransmitFile("~/images/test.jpg");
}
// readStream is the stream you need to read
// writeStream is the stream you want to write to
private void ReadWriteStream(Stream readStream, Stream writeStream)
{
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer, 0, Length);
// write the required bytes
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = readStream.Read(buffer, 0, Length);
}
readStream.Close();
writeStream.Close();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
string saveTo = @"location to save new image";
FileStream writeStream = File.Open(saveTo, FileMode.Create, FileAccess.ReadWrite);
ReadWriteStream(fs, writeStream);
//Response.Clear();
//Response.TransmitFile("~/images/test.jpg");
}
}
}
这是计时器的 .aspx 代码
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel4" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="5000" >
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
这是查看器页面的代码:
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image1" runat="server" />
<img src="/video.aspx" id="the_image" alt="" />
<script type="text/javascript" language="javascript">
function refreshImage() {
objIMG = document.getElementById('the_image');
objIMG.src = objIMG.src.substr(0, objIMG.src.indexOf('&nocache=')); +'&nocache=' + Math.random();
}
$(document).ready(function () {
setInterval(refreshImage, 1000);
})
</script>
</div>
</form>
</body>
它不是每 5 秒保存一次或者它没有刷新图像,我不知道是哪个问题。有人可以帮忙吗?
【问题讨论】: