使用 Ajax 从服务器向客户端发送进度消息

发布于 2024-07-26 01:42:25 字数 507 浏览 2 评论 0原文

我正在使用 UpdatePanel 触发按钮单击事件,这会在指定文件夹中保存大约 100 多个文件。 我希望服务器更新客户端有关正在保存的文件的状态和数量。

protected void btnSave_Click(...){    
  var filesToSave = GetFilesToSave();
  foreach(var fileToSave in filesToSave){
    SaveProcessedFile(fileToSave);//It takes almost 30seconds to save a file
    UpdateStatusOnClient(fileToSave); //Don;t know what should be done here???????????
  }
}

我正在寻找“UpdateStatusOnClient”的一些实现,从中我可以向客户端发送所需的消息,或者异步更新标签消息,以便客户端知道保存文件的进度和状态。

谢谢。

I am using UpdatePanel to trigger a button click event, which saves some 100+ files on a designated folder.
I want the server to update the client about the status and count of files being saved.

protected void btnSave_Click(...){    
  var filesToSave = GetFilesToSave();
  foreach(var fileToSave in filesToSave){
    SaveProcessedFile(fileToSave);//It takes almost 30seconds to save a file
    UpdateStatusOnClient(fileToSave); //Don;t know what should be done here???????????
  }
}

I am looking for some implementation of "UpdateStatusOnClient" from where i can send a desired message to client, or update a label message asynchronously so that the client knows the progress and status of files being saved.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲喜皆因你 2024-08-02 01:42:25

这没那么容易……你不能主动从服务器向客户端发送一些东西。 只有客户端可以发出查询状态的请求。

现在您已经有一个请求正在运行(单击按钮)。 但这只有在保存完 100 个文件后才会完成。 理论上,您可以通过写入响应来发送少量数据,然后刷新它以确保数据已传输。 我从来没有尝试过,而且我不知道有任何 Ajax 客户端可以处理这样的响应。

我要做的是:将当前状态保存在 ASP.NET 会话对象中,然后对计时器发出另一个 Ajax 请求,每 X 秒查询一次状态。 请注意,执行此操作时按钮上的请求仍在运行(这可能会导致问题)。 您可能想改为触发异步操作。

It's not that easy... You can't actively send something from the server to the client. Only the client can make a request to query the status.

Now you already have a request running (the click on the button). But that will only finish once the 100 files have been saved. In theory, you could send a small bit of data by writing to the response and then flushing it to make sure that data is transmitted. I've never tried that, and I don't know any Ajax client that could deal with such a response.

What I would do is this: Save the current status in the ASP.NET session object, and then make another Ajax request on a timer to query the status every X seconds. Be aware that the request on the button is still running while you do this (it could cause problems). You may want to trigger an async action instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文