This question is not fully answered, please feel free to contribute!
I am trying to display a simple progress bar while a large form is submitting.
The form contains a dozen fields, as well as fields for downloading files, where the user can select an image. Then, when he clicks the Create button, a form with data and images is submitted, and the object is created in the database. (just one click to send the form and photos).
Everything is working fine, but I want to display a progress bar during the sending process.
I found many tutorials explaining how to display a progress bar, but I do not find anyone to explain how to display a progress bar showing the percentage of work completed by a method , i.e. d like to see 10%, 25%, etc. during the shipping process.
So basically, this is what I did: (this is an ASP.NET MVC3 project)
@model MyModel @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "target-form", enctype = "multipart/form-data" })) { //some Html.TextBoxFor() and other @Html.DropDownListFor @Html.TextBoxFor(m => m.File, new { type = "file"}) <input type="submit" value="Create" class="submitButton" /> <div id="progressBar"></div> }
And the base controller
[HttpPost] public ActionResult Create(MyModel model) { if (ModelState.IsValid) { DALLayer dal = new DALLayer() dal.AddEntity(model); return RedirectToAction("Index", "Home"); } return null; }
Is it possible to convert my last <div> to a progressBar displaying the status of the loading progressBar ?
Here are my requirements:
- There is no plugin (this is an individual project, I want to understand how to do it myself).
- Cross compatible, IE8 + (if possible)
- jQuery is fine but no flash.
Many thanks!
UPDATE 1 Here is JSFIDDLE where I am trying to adapt this link , but to no avail ... If you think you can help, please!
UPDATE 2 Well, I used acarlon's answer to submit my form using XMLHttpRequest , and the data was sent correctly to the controller. However, the ProgressBar still does not appear!
I will simply replace the data passed to the controller:
formData = new FormData(document.getElementById("target-form") ); xhr.open("POST", "@Url.Action("MyMethod", "MyController")", true );
and try a few different headers, for example:
xhr.setRequestHeader("X-File-Name", $('#Files_0__File')[0].files[0].name); xhr.setRequestHeader("X-File-Size", $('#Files_0__File')[0].files[0].size); xhr.setRequestHeader("X-File-Type", $('#Files_0__File')[0].files[0].type);
(He was hard-coded here to be sure that it has good values. I will do this in a loop when it will be easier with him.)
And when I submit my form, XMLHttpRequest send has the following fields:
readyState: 4
status: 0 <= should be 200, right?
And in the error handler , I have the following values:
uploaded: 0 | total: 883526
type: "error"
So, the data is being transferred to my controller, but I canβt display this damn progress indicator ...