I added a drop-down library to my project when I picked a bunch of files.
And press the file upload button. The code only uploads one file and ignores the rest of the files.
It does not save the name and extension of files in the database. I don’t know where the error is ??
Note: All files are successfully uploaded if the save code is cleared
- public ActionResult Upload()
- {
- FILES_TABLE ft = new FILES_TABLE();
-
- bool isSavedSuccessfully = true;
- string fName = "";
- try
- {
- foreach (string fileName in Request.Files)
- {
- HttpPostedFileBase file = Request.Files[fileName];
- fName = file.FileName;
- if (file != null && file.ContentLength > 0)
- {
- var path = Path.Combine(Server.MapPath("~/MyImages"));
- string pathString = System.IO.Path.Combine(path.ToString());
- string fileName1 = Path.GetFileName(file.FileName);
- string ext1 = System.IO.Path.GetExtension(file.FileName);
- bool isExists = System.IO.Directory.Exists(pathString);
- if (!isExists) System.IO.Directory.CreateDirectory(pathString);
- var uploadpath = string.Format("{0}\\{1}", pathString, file.FileName);
- file.SaveAs(uploadpath);
-
- ft.FileName = fileName1;
- ft.FileExt = ext1;
- _DB.FILES_TABLE.Add(ft);
- _DB.SaveChanges();
- }
- }
- }
- catch (Exception ex)
- {
- isSavedSuccessfully = false;
- ViewBag.Err = "{0} Exception caught." + ex;
- }
- if (isSavedSuccessfully)
- {
- return RedirectToAction("Index");
- }
- else
- {
- return Json(new
- {
- Message = "Error in saving file"
- });
- }
-
- }