views:

67

answers:

2

Hi guys

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).

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???

Cheers Anthony

A: 

You'll probably want to look into implementing Asynchronous requests in ASP.NET MVC, or something similar.

Andrew Lewis
I don't think this is right because the time taken in the upload is not the processing of data once the data has arrived on the server its the sending of the data to the server. As far as I know this happens well before Asynchronous requests logic ASP.NET MVC can be used.
vdhant
+1  A: 

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.

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.

[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.

Luke Sampson
When you say "When you've read a chunk of the uploaded file on the server, you would call" are you saying this would go in the action??? If this is the case as far as I know the action only gets called when all data is received. Hence where would the SetStatus call be placed?
vdhant
Just wondering if you have had any thoughts on this?
vdhant
Yep you're absolutely right - a normal action will only be executed once all the data is received. Possibly you could use an AsyncController to get around this, although I haven't tried this. I went with a custom IHttpHandler myself. Either way, you'll need to find or write some code to parse the multipart form data in chunks and set the upload status, and then there's all the javascript as well. PITA!Did you see this question for some possible alternatives? http://stackoverflow.com/questions/977995/how-can-i-upload-a-file-via-asp-net-mvc-and-show-a-progress-bar
Luke Sampson