I have the following code which is used to upload large files (~6MB) to an ASP .NET MVC2 application. After the file is uploaded it can take 5 minutes to return a response from the controller.
During this time, IE9 does not show any sign that processing is not yet completed, i.e. user can select other file and re-post.
How do you inform the user that an operation is not completed and disable the submit button until the old file processing is completed?
<% using (Html.BeginForm("InvoiceImport", "Grid",
FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
<input name="uploadFile" type="file" />
<input type="submit" name='invoice' value='Read'/>
<%} %>
Controller:
[AcceptVerbs(HttpVerbs.Post)]
[Authorize]
ActionResult ImportFromFile(HttpPostedFileBase uploadFile)
{
Server.ScriptTimeout = 30 * 60;
var res = ImportFromFile(uploadFile.InputStream); // takes lot of time
TempData["Message"] = uploadFile.FileName + " Readed records " + res;
return RedirectToAction("Complete");
}
Update
I looks like the most important issue is to prevent duplicate submits. I tried code below to prevent duplicate submits but for unknown reason Session["Upload"] is null if submit button is pressed in second time. How to prevent duplicate submits ?
View added
<% if (TempData["Message"]!=null) { %>
setTimeout( function() {
showMessage ( '<%= TempData["Message"] as string %>');
}, 300 );
<% } %>
Controller
ActionResult ImportFromFile(HttpPostedFileBase uploadFile)
{
if (Session["Upload"] != null)
{
// todo: why this point is never reached ?
TempData["Message"] = Session["Upload"] + " Please wait file is being processed";
return View();
}
Session.Add("Upload", uploadFile.FileName);
Server.ScriptTimeout = 30 * 60;
ImportFromFile(uploadFile.InputStream);
TempData["Message"] = uploadFile.FileName + " completed";
Session.Remove("Upload");
return RedirectToAction("Complete");
}