In this article we are going to learn how to implement jquery progress bar in File Upload control in ASP MVC.
Step 1
First we have to create an Asp Mvc project in visual studio. To do that, Open Visual Studio -> New Project -> A new dialog window will appear -> In left pane select C# ->select Web -> Select "ASP.NET MVC 4 Application" name your project and click ok.
Again a new dialog window will appear. In that select "Internet Application"
Please refer to the below image for your reference.
Step 2
Now you can see your project files in right corner of visual studio. Please refer to the below image for your reference
Step 3
Open controller folder. In that we have "Home" and "Account" controller default. It was created while creating a new project in asp mvc.
If you open "Home" controller , you can see some default code in that. Please refer to the below image for your reference.
Step 4
In view folder we can see the "Home" folder and can see the "Index" view. It was also created when we created a project.
If you want to create a view for your action result , just right click on your action result and click "Add View"
Please refer to the below image for your reference.
Step 5
Now let's change the index view code as per our requirement.
Please just copy and paste the code in your index view.
Note
Here I have added the "ProgressBar" css and script file from CDN(Content Delivery Network)
- @{
- ViewBag.Title = "Home Page";
- }
- <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
- rel="stylesheet"> @*I got this link from Tutorials point*@
- <link href="../../Content/bootstrap.css" rel="stylesheet" type="text/css" /> @*Download Bootstrap from Nuget Package manager*@
- <link href="../../Content/bootstrap-theme.css" rel="stylesheet" type="text/css" />
- <style>
- .ui-widget-header
- {
- background: #cedc98;
- border: 1px solid #DDDDDD;
- color: #333333;
- font-weight: bold;
- }
- .progress-label
- {
- position: absolute;
- left: 50%;
- top: 13px;
- font-weight: bold;
- text-shadow: 1px 1px 0 #fff;
- }
- .red
- {
- color: red;
- }
- </style>
- <div class="container">
- <h1>
- File Upload Demo</h1>
- <div id="FileBrowse">
- <div class="row">
- <div class="col-sm-4">
- <input type="file" id="Files" />
- </div>
- <div class="col-sm-2">
- <input type="button" id="UploadBtn" class="btn btn-danger" value="Upload" />
- </div>
- </div>
- </div>
- <div class="row">
- <div class="col-sm-4">
- <div id="progressbar-5">
- <div class="progress-label">
- </div>
- </div>
- </div>
- </div>
- <br />
- <div class="row">
- <div class="col-sm-6">
- <table class="table" id="ListofFiles">
- <tr>
- <th>
- Files
- </th>
- <th>
- Action
- </th>
- </tr>
- </table>
- </div>
- </div>
- <br />
- <br />
- <br />
- <br />
- </div>
- @section scripts{
- <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
- <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
- <script>
-
- $('#UploadBtn').click(function () {
-
- var fileUpload = $("#Files").get(0);
- var files = fileUpload.files;
-
- var fileData = new FormData();
-
- for (var i = 0; i < files.length; i++) {
- fileData.append(files[i].name, files[i]);
- }
- $.ajax({
- url: '/Home/UploadFiles',
- type: "POST",
- contentType: false,
- processData: false,
- data: fileData,
- async: false,
- success: function (result) {
- if (result != "") {
- $('#FileBrowse').find("*").prop("disabled", true);
- LoadProgressBar(result);
- }
- },
- error: function (err) {
- alert(err.statusText);
- }
- });
-
- });
-
- function LoadProgressBar(result) {
- var progressbar = $("#progressbar-5");
- var progressLabel = $(".progress-label");
- progressbar.show();
- $("#progressbar-5").progressbar({
-
- change: function () {
- progressLabel.text(
- progressbar.progressbar("value") + "%");
- },
- complete: function () {
- progressLabel.text("Loading Completed!");
- progressbar.progressbar("value", 0);
- progressLabel.text("");
- progressbar.hide();
- var markup = "<tr><td>" + result + "</td><td><a href='#' onclick='DeleteFile(\"" + result + "\")'><span class='glyphicon glyphicon-remove red'></span></a></td></tr>";
- $("#ListofFiles tbody").append(markup);
- $('#Files').val('');
- $('#FileBrowse').find("*").prop("disabled", false);
- }
- });
- function progress() {
- var val = progressbar.progressbar("value") || 0;
- progressbar.progressbar("value", val + 1);
- if (val < 99) {
- setTimeout(progress, 25);
- }
- }
- setTimeout(progress, 100);
- }
-
- function DeleteFile(FileName) {
-
-
- }
-
- </script>
- }
Let;s understand the code now, I have used one file input control, html input button. Find the code here
- <div id="FileBrowse">
- <div class="row">
- <div class="col-sm-4">
- <input type="file" id="Files" />
- </div>
- <div class="col-sm-2">
- <input type="button" id="UploadBtn" class="btn btn-danger" value="Upload" />
- </div>
- </div>
- </div>
- <div class="row">
- <div class="col-sm-4">
- <div id="progressbar-5">
- <div class="progress-label">
- </div>
- </div>
- </div>
- </div>
I have used one html table to show the files list.find the code here.
- <div class="row">
- <div class="col-sm-6">
- <table class="table" id="ListofFiles">
- <tr>
- <th>
- Files
- </th>
- <th>
- Action
- </th>
- </tr>
- </table>
- </div>
- </div>
In jquery I have written upload button click event and passed the file to controller using ajax method.
Please find the code below,
- $('#UploadBtn').click(function () {
- var fileUpload = $("#Files").get(0);
- var files = fileUpload.files;
-
- var fileData = new FormData();
-
- for (var i = 0; i < files.length; i++) {
- fileData.append(files[i].name, files[i]);
- }
- $.ajax({
- url: '/Home/UploadFiles',
- type: "POST",
- contentType: false,
- processData: false,
- data: fileData,
- async: false,
- success: function (result) {
- if (result != "") {
- $('#FileBrowse').find("*").prop("disabled", true);
- LoadProgressBar(result);
- }
- },
- error: function (err) {
- alert(err.statusText);
- }
- });
- });
Let's create the "UploadFiles" action result in "Home" controller to receive the file from request and save the file in our "Uploads" folder.
Please find the code below for your reference.
- public ActionResult UploadFiles()
- {
- string FileName="";
- HttpFileCollectionBase files = Request.Files;
- for (int i = 0; i < files.Count; i++)
- {
-
-
-
- HttpPostedFileBase file = files[i];
- string fname;
-
-
- if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
- {
- string[] testfiles = file.FileName.Split(new char[] { '\\' });
- fname = testfiles[testfiles.Length - 1];
- }
- else
- {
- fname = file.FileName;
- FileName = file.FileName;
- }
-
-
- fname = Path.Combine(Server.MapPath("~/Uploads/"), fname);
- file.SaveAs(fname);
- }
- return Json(FileName, JsonRequestBehavior.AllowGet);
- }
In Button Click event we are calling the function "LoadProgressbar" to load the progress bar and bind the fields in the table.
All the code wrote in "LoadProgressBar", please find the code for your reference.
- function LoadProgressBar(result) {
- var progressbar = $("#progressbar-5");
- var progressLabel = $(".progress-label");
- progressbar.show();
- $("#progressbar-5").progressbar({
-
- change: function () {
- progressLabel.text(
- progressbar.progressbar("value") + "%");
- },
- complete: function () {
- progressLabel.text("Loading Completed!");
- progressbar.progressbar("value", 0);
- progressLabel.text("");
- progressbar.hide();
- var markup = "<tr><td>" + result + "</td><td><a href='#' onclick='DeleteFile(\"" + result + "\")'><span class='glyphicon glyphicon-remove red'></span></a></td></tr>";
- $("#ListofFiles tbody").append(markup);
- $('#Files').val('');
- $('#FileBrowse').find("*").prop("disabled", false);
- }
- });
- function progress() {
- var val = progressbar.progressbar("value") || 0;
- progressbar.progressbar("value", val + 1);
- if (val < 99) {
- setTimeout(progress, 25);
- }
- }
- setTimeout(progress, 100);
- }
Now let's run our project. Please find the below result for your reference.
Thanks for reading my article. If you have any comments or queries, please mention in the comment box.