Step 1

Create a View with a file control to upload the file and a table to display the list of uploaded files. In this View, we have one textbox for username and a button to complete the upload process.

  1. <h3>Upload File(s)</h3>
  2. <style>
  3. .red {
  4. color: red;
  5. }
  6. </style>
  7. <form id="uploader">
  8. <div class="row">
  9. <div class="col-sm-6">
  10. User Name : <input type="text" id="txtuploader" />
  11. <br /> <br />
  12. <input id="fileInput" type="file" multiple>
  13. <br /> <br />
  14. <table class="table" id="FilesList" style="visibility:hidden">
  15. <tr>
  16. <th>
  17. Attachment(s)
  18. </th>
  19. <th>
  20. Action
  21. </th>
  22. </tr>
  23. </table>
  24. <input type="button" id="btnupload" value="Upload" style="float:right" />
  25. </div>
  26. </div>
  27. </form>

Step 2

The jQuery functions will perform the following functionalities:

  • Move uploaded files into a list on file change event.
  • chkatchtbl() helps to change the visibility of the files in the list based on the availability of the files.
  • DeleteFile() will remove selected files in the list on the click of "Remove" button
  • On click of the Upload button, it will call the controller using jQuery AJAX POST method.
  1. <script>
  2. var formdata = new FormData(); //FormData object
  3. $(document).ready(function () {
  4. $("#fileInput").on("change", function () {
  5. var fileInput = document.getElementById('fileInput');
  6. //Iterating through each files selected in fileInput
  7. for (i = 0; i < fileInput.files.length; i++) {
  8. var sfilename = fileInput.files[i].name;
  9. let srandomid = Math.random().toString(36).substring(7);
  10. formdata.append(sfilename, fileInput.files[i]);
  11. var markup = "<tr id='" + srandomid + "'><td>" + sfilename + "</td><td><a href='#' onclick='DeleteFile(\"" + srandomid + "\",\"" + sfilename +
  12. "\")'><span class='glyphicon glyphicon-remove red'></span></a></td></tr>"; // Binding the file name
  13. $("#FilesList tbody").append(markup);
  14. }
  15. chkatchtbl();
  16. $('#fileInput').val('');
  17. });
  18. $("#btnupload").click(function () {
  19. formdata.append('uploadername', $('#txtuploader').val());
  20. $.ajax({
  21. url: '/Home/UploadFiles',
  22. type: "POST",
  23. contentType: false, // Not to set any content header
  24. processData: false, // Not to process data
  25. data: formdata,
  26. async: false,
  27. success: function (result) {
  28. if (result != "") {
  29. alert(result);
  30. }
  31. },
  32. error: function (err) {
  33. alert(err.statusText);
  34. }
  35. });
  36. });
  37. });
  38. function DeleteFile(Fileid, FileName) {
  39. formdata.delete(FileName)
  40. $("#" + Fileid).remove();
  41. chkatchtbl();
  42. }
  43. function chkatchtbl() {
  44. if ($('#FilesList tr').length > 1) {
  45. $("#FilesList").css("visibility", "visible");
  46. } else {
  47. $("#FilesList").css("visibility", "hidden");
  48. }
  49. }
  50. </script>

Step 3

Finally, in the Controller, it gets the all the uploaded files. When we enter the username, it stores the uploaded files into a temporary location.

  1. public ActionResult UploadFiles()
  2. {
  3. string uname = Request["uploadername"];
  4. HttpFileCollectionBase files = Request.Files;
  5. for (int i = 0; i < files.Count; i++)
  6. {
  7. HttpPostedFileBase file = files[i];
  8. string fname;
  9. // Checking for Internet Explorer
  10. if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
  11. {
  12. string[] testfiles = file.FileName.Split(new char[] { '\\' });
  13. fname = testfiles[testfiles.Length - 1];
  14. }
  15. else
  16. {
  17. fname = file.FileName;
  18. }
  19. // Get the complete folder path and store the file inside it.
  20. fname = Path.Combine(Server.MapPath("~/Uploads/"), fname);
  21. file.SaveAs(fname);
  22. }
  23. return Json("Hi, " + uname + ". Your files uploaded successfully", JsonRequestBehavior.AllowGet);
  24. }
  25. }