gpt4 book ai didi

c# - 我如何每 x 秒使用 javascript 调用一个 C# 函数?

转载 作者:行者123 更新时间:2023-12-03 12:03:28 24 4
gpt4 key购买 nike

我有一个将图像从 IPcamera 保存到文件流的项目。我有另一个项目,它从文件流中获取图像并将其另存为 jpg,然后在 Web 表单中显示该图像。我添加了一个 javascript 脚本来每 x 秒刷新一次图像,但问题是新图像仅在页面加载时才会保存。之所以这样做,是因为我将 C# 代码放在“Page_Load()”函数中。我想知道是否有一种方法可以在 C# 中创建一个具有保存图像的代码部分的函数,然后在刷新图像时每 x 秒调用该函数?因为现在由于没有保存新图像,所以它只是刷新到相同的图像。我编写了一个每隔x秒刷新整个页面的代码,但我觉得这种方式会更好。我使用的是 Visual Studio 2010。

下面是提到的第二个项目的 C# 代码:

namespace PlayVideo 
public partial class Video : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

string saveTo = @"place to save";
FileStream writeStream = new FileStream(saveTo, FileMode.Create, FileAccess.ReadWrite);

using (FileStream fs = File.Open(@"Place where file is saved", FileMode.Open, FileAccess.ReadWrite, FileShare.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();
}
}

viewer.aspx 页面的代码是:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="viewer.aspx.cs"        Inherits="PlayVideo.viewer" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title></title>

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 60px" id="div1">

<img src="/video.aspx" id="the_image" alt="" /><br />

<script type="text/javascript" language="javascript">

function refreshImage() {
$("#the_image").attr('src', 'video.aspx');
setTimeout(refreshImage, x ms);
}

$(document).ready(function () {
setInterval(refreshImage, x ms);
})
</script>

</div>
</form>
</body>
</html>

有办法做到这一点吗?我已经阅读过有关客户端与服务器端的内容,但由于我已经每 x 秒刷新一次图像,这有关系吗?

最佳答案

创建一个用属性 [WebMethod] 标记的公共(public)静态方法,并使用对该方法的 AJAX 调用。示例:How to send and retrieve data from web method using JQuery Ajax Call?

关于c# - 我如何每 x 秒使用 javascript 调用一个 C# 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25291789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com