问题
I have an application that allows users to upload audio files. On some very large files (1-2GB) I have run into an issue that the users session/authentication will time out, so when the file finishes uploading (which it does successfully) the user is forced to log in again.
I am using a third party tool called FileUploader on the front end and an ashx on the server side to handle the incoming file.
Does anyone have suggestions on how to keep the session/authentication from timing out while the file is uploading? Basically I would like the time that the file is being uploaded to be treated as if the user is actively using the site.
回答1:
How about using FineUploader's progress event and make an Ajax call to a new WebService method that refreshes session every time progress is fired:
[System.Web.Script.Services.ScriptService]
public class SessionAlive : System.Web.Services.WebService
{
[WebMethod]
public void UpdateSession()
{
HttpContext.Current.Session["tempVariable"] = DateTime.Now;
}
}
Or you could use a timeout that kicks off when the upload starts:
$(document).ready(function() {
setTimeout(updateSession, 1000*60);//Timeout is 1 min
});
function updateSession() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "SessionAlive.asmx/UpdateSession",
data: "{}",
dataType: "json"
});
setTimeout(updateSession, 1000 * 60);
}
Reference
来源:https://stackoverflow.com/questions/24541130/how-to-keep-a-session-alive-while-a-file-uploads