Just wondering if its possible to get the status of a request from another request?
只是想知道是否可以从另一个请求获取请求的状态?
To help provide some context, I want to use ajax to upload some files to the server. When the upload is started I want triggered another ajax request that checks to see how much of the file has been uploaded (or in other words how big was the other request and how far through receiving the request is the server).
为了帮助提供一些上下文,我想使用ajax将一些文件上传到服务器。当上传开始时,我想要触发另一个ajax请求,该请求检查已经上载了多少文件(或者换句话说,另一个请求有多大,以及接收请求到服务器的程度)。
I was thinking that I could use a guid or something similar to ensure that I am checking the status of the right request. But how do I check the status of another request???
我以为我可以使用guid或类似的东西来确保我正在检查正确请求的状态。但是如何检查另一个请求的状态???
Cheers Anthony
2 个解决方案
#1
1
It's definitely possible. There's no built in support for checking the status of another request in ASP.NET MVC as far as I'm aware but it's not hard to implement.
这绝对是可能的。据我所知,没有内置支持在ASP.NET MVC中检查另一个请求的状态,但实现起来并不困难。
public static UploadStatusHelper {
static Dictionary<string, int> uploads = new Dictionary<string, int>();
static object lockObj = new object();
public static void SetStatus(string key, int bytesComplete) {
lock(lockObj) {
uploads[key] = bytesComplete;
}
}
public static void GetStatus(string key) {
lock(lockObj) {
int bytesComplete = 0;
if(uploads.TryGetValue(key, out bytesComplete) {
return bytesComplete;
}
return 0;
}
}
}
When you've read a chunk of the uploaded file on the server, you would call
当您在服务器上读取上传文件的一大块时,您可以打电话
UploadStatusHelper.SetStatus(theKey, bytesRead);
Then using using Ajax you can initiate an HTTP POST request periodically to an action which returns the progress, e.g.
然后使用Ajax,您可以定期向一个返回进度的动作发起HTTP POST请求,例如,
[HttpPost]
public ActionResult GetProgress(string key)
int bytesRead = UploadStatusHelper.GetStatus(key);
return Json(new { bytesRead = bytesRead });
}
You'd probably want to extend this helper to store the total length of the file and return a percentage complete, but this is just meant to show you how it could be done.
您可能希望扩展此帮助程序以存储文件的总长度并返回完成百分比,但这只是为了向您展示如何完成它。
#2
0
You'll probably want to look into implementing Asynchronous requests in ASP.NET MVC, or something similar.
您可能希望研究在ASP.NET MVC中实现异步请求,或类似的东西。
#1
1
It's definitely possible. There's no built in support for checking the status of another request in ASP.NET MVC as far as I'm aware but it's not hard to implement.
这绝对是可能的。据我所知,没有内置支持在ASP.NET MVC中检查另一个请求的状态,但实现起来并不困难。
public static UploadStatusHelper {
static Dictionary<string, int> uploads = new Dictionary<string, int>();
static object lockObj = new object();
public static void SetStatus(string key, int bytesComplete) {
lock(lockObj) {
uploads[key] = bytesComplete;
}
}
public static void GetStatus(string key) {
lock(lockObj) {
int bytesComplete = 0;
if(uploads.TryGetValue(key, out bytesComplete) {
return bytesComplete;
}
return 0;
}
}
}
When you've read a chunk of the uploaded file on the server, you would call
当您在服务器上读取上传文件的一大块时,您可以打电话
UploadStatusHelper.SetStatus(theKey, bytesRead);
Then using using Ajax you can initiate an HTTP POST request periodically to an action which returns the progress, e.g.
然后使用Ajax,您可以定期向一个返回进度的动作发起HTTP POST请求,例如,
[HttpPost]
public ActionResult GetProgress(string key)
int bytesRead = UploadStatusHelper.GetStatus(key);
return Json(new { bytesRead = bytesRead });
}
You'd probably want to extend this helper to store the total length of the file and return a percentage complete, but this is just meant to show you how it could be done.
您可能希望扩展此帮助程序以存储文件的总长度并返回完成百分比,但这只是为了向您展示如何完成它。
#2
0
You'll probably want to look into implementing Asynchronous requests in ASP.NET MVC, or something similar.
您可能希望研究在ASP.NET MVC中实现异步请求,或类似的东西。