Javascript用于使用VS2010刷新图像

本文关键字:刷新 图像 VS2010 用于 Javascript | 更新日期: 2023-09-27 18:15:45

我有一个项目,将图像从IPcamera保存到文件流。我有另一个项目,从文件流中获取图像,并将其保存为jpg格式,然后在webform中显示图像。下面是第二个项目的c#代码:

namespace PlayVideo 
public partial class Video : System.Web.UI.Page
{
FileStream fs = File.Open(@"location of filestream");

protected void Page_Load(object sender, EventArgs e)
{
string saveTo = @"place to save";
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();
}

然后我添加了一个查看器页面来显示图像并使用javascript来刷新图像,下面是代码:

<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image1" runat="server"  />
<img src="/video.aspx" id="the-image" alt="" />
    <script language="JavaScript" type="text/javascript">
        function refreshImage() {
            $("#the-image").attr('src', 'video.aspx');
            setTimeout("refreshImage();", 1000);
        }
        $(document).ready(function () {
            setTimeout("refreshImage();", 1000);
                        })

</script> 

</div>
</form>

我的问题是图像从不刷新。我试过把javascript放在html的不同部分,甚至尝试了很多不同的javascript方法在网上找到。我现在认为我的c#代码部分有问题,而不是javascript,但我不知道。有人能帮帮我吗?

Javascript用于使用VS2010刷新图像

下面是一些代码:http://jsfiddle.net/39yN8/1/

// Declaring a function that may be called later.
function refreshImage()
{
    // Get the img DOM element. Only your img tag with 'myIMG' id is caught.
    objIMG = document.getElementById('myIMG');
    // Refresh the image src attribute.
    // There is some trick to change the original URL string to hack the browser cache.
    objIMG.src = objIMG.src.substr(0, objIMG.src.indexOf('&nocache=')); + '&nocache=' + Math.random();
}
$(document).ready(function () // When page is loaded...
{
    // Call refreshImage function every 1000 milliseconds.
    setInterval(refreshImage, 1000);
})

基本上,你的浏览器缓存有一些问题,所以我在URL中添加了一个带有随机数的"no-cache"参数,以欺骗你的浏览器。