<input id="file" class="form-control-file" type="file" name="file" placeholder="Document Upload" />
$("#file").change(function () { var formData = new FormData(); var totalFiles = document.getElementById("file").files.length; for (var i = 0; i < totalFiles; i++) { var file = document.getElementById("file").files[i]; formData.append("file", file); } $.ajax({ type: "POST", url: '/Admin/PreviewUpload', data: formData, dataType: 'json', contentType: false, processData: false, success: function (response) { alert('succes!!'); }, error: function (error) { alert("errror"); } }); });
[HttpPost] public ActionResult PreviewUpload() { if (Request.Files.Count > 0) { foreach (string files in Request.Files) { var _file = Request.Files[files]; FileInfo Fi = new FileInfo(Path.GetFileName(_file.FileName)); string fileExtention = Fi.Extension; if (_file != null) { if (fileExtention == ".PDF") { string fileName = Path.GetFileName(_file.FileName); if (_file.ContentLength <= 120000000) { _file.SaveAs(Server.MapPath("~/PreviewPDF/" + fileName)); } string path = "/PreviewPDF/" + Path.GetFileName(_file.FileName); ViewData["error"] = path; return Json(new { Success = path }); } else { return Json(new { fileError = "Only Support PDF" }); } } else { return Json(new { error = "Please Select the file" }); } } } return Json(new { error = "Please Select the file" }); }
dataType: 'json'
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)