Saturday, August 10, 2019

How to Upload Files Asynchronously using ASP.NET MVC4 application

Step - 1: Create New Project.

Go to File > New > Project > Select asp.net MVC4 web application > Entry Application Name > Click OK > Select Internet Application > Select view engine Razor > OK

Step-2: Add a new Controller.

Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > Select Templete "empty MVC Controller"> Add.

Step-3: Add new action into your controller for Get Method.

Here I have added "AsyncUpload" Action into "Upload" Controller. Please write this following code

  1. public ActionResult AsyncUpload()
  2. {
  3. return View();
  4. }

Step-4: Add view for the Action & design.

Right Click on Action Method (here right click on form action) > Add View... > Enter View Name > Select View Engine (Razor) > Check "Create a strong-typed view" > Select your model class > Add.

Step-5: Add jQuery code for upload file to the server.

Jquery Code 
  1. <script src="http://malsup.github.com/jquery.form.js"></script>
  2. <script>
  3. (function () {
  4. var bar = $('.progress-bar');
  5. var percent = $('.progress-bar');
  6. var status = $('#status');
  7.  
  8. $('form').ajaxForm({
  9. beforeSend: function () {
  10. status.empty();
  11. var percentValue = '0%';
  12. bar.width(percentValue);
  13. percent.html(percentValue);
  14. },
  15. uploadProgress: function (event, position, total, percentComplete) {
  16. var percentValue = percentComplete + '%';
  17. bar.width(percentValue);
  18. percent.html(percentValue);
  19. },
  20. success: function (d) {
  21. var percentValue = '100%';
  22. bar.width(percentValue);
  23. percent.html(percentValue);
  24. $('#fu1').val('');
  25. alert(d);
  26. },
  27. complete: function (xhr) {
  28. status.html(xhr.responseText);
  29. }
  30. });
  31. })();
  32. </script>
Complete View 
  1. @{
  2. ViewBag.Title = "Async File Upload";
  3. }
  4.  
  5. <h2>Async File Upload</h2>
  6.  
  7. @using (Ajax.BeginForm("AsyncUpload", "Upload", new AjaxOptions() { HttpMethod = "POST" }, new { enctype="multipart/form-data"}))
  8. {
  9. @Html.AntiForgeryToken()
  10. <input type="file" name="files" id="fu1"/>
  11. <input type="submit" value="Upload File" />
  12. }
  13.  
  14. <div class="progress">
  15. <div class="progress-bar">0%</div>
  16. </div>
  17. <div id="status"></div>
  18. <style>
  19. .progress {
  20. position:relative;
  21. width:400px;
  22. border:1px solid #ddd;
  23. padding:1px;
  24. }
  25. .progress-bar {
  26. width:0px;
  27. height:20px;
  28. background-color:#57be65;
  29. }
  30. </style>
  31. @section scripts{
  32. <script src="http://malsup.github.com/jquery.form.js"></script>
  33. <script>
  34. (function () {
  35. var bar = $('.progress-bar');
  36. var percent = $('.progress-bar');
  37. var status = $('#status');
  38.  
  39. $('form').ajaxForm({
  40. beforeSend: function () {
  41. status.empty();
  42. var percentValue = '0%';
  43. bar.width(percentValue);
  44. percent.html(percentValue);
  45. },
  46. uploadProgress: function (event, position, total, percentComplete) {
  47. var percentValue = percentComplete + '%';
  48. bar.width(percentValue);
  49. percent.html(percentValue);
  50. },
  51. success: function (d) {
  52. var percentValue = '100%';
  53. bar.width(percentValue);
  54. percent.html(percentValue);
  55. $('#fu1').val('');
  56. alert(d);
  57. },
  58. complete: function (xhr) {
  59. status.html(xhr.responseText);
  60. }
  61. });
  62. })();
  63. </script>
  64. }

Step-6: Add another action into your controller for Upload files to the server.

Here I have added "AsyncUpload" Action into "Upload" Controller for POST Action. Please write this following code

  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. public ActionResult AsyncUpload(IEnumerable<HttpPostedFileBase> files)
  4. {
  5. int count = 0;
  6. if (files != null)
  7. {
  8. foreach (var file in files)
  9. {
  10. if (file != null && file.ContentLength > 0)
  11. {
  12. var fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
  13. var path = Path.Combine(Server.MapPath("~/UploadedFiles"), fileName);
  14. file.SaveAs(path);
  15. count++;
  16. }
  17. }
  18. }
  19. return new JsonResult { Data = "Successfully " + count+ " file(s) uploaded" };
  20. }

Step-7: Create a folder for Save uploaded files.

Right Click on ProjectName under Solution Explorer > Add > New Folder > enter folder name.

Step-8: Run Application.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...