public ActionResult UploadFiles(IFormFile[] files) { _logger.LogInformation("ChildrenLists page is called to Upload single or multiples file in database"); try { ChildrenDetailsContext db = new ChildrenDetailsContext(); if (files != null) { foreach (var file in files) { // extract only the filename var fileName = Path.GetFileName(file.FileName); // extract the file content to byte array var content = new byte[file.Length]; // reads the content from stream //file.InputStream.Read(content, 0, file.ContentLength); file.OpenReadStream(); //get file extesion var fileExtension = Path.GetExtension(fileName); //save file name as uniqe var uniqueFileName = Guid.NewGuid().ToString(); ChildrenFileUpload objChildrenFileUpload = new ChildrenFileUpload { FileName = uniqueFileName, UploadDate = DateTime.Now, FileContent = content }; db.ChildrenFileUpload.Add(objChildrenFileUpload); db.SaveChanges(); } } return(RedirectToAction("ChildrenLists")); } catch (Exception) { throw; } }
public ActionResult UploadFiles(HttpPostedFileBase[] files) { try { ChildrenDetailsEntities db = new ChildrenDetailsEntities(); //if (files != null) //{ // //Ensure model state is valid // if (ModelState.IsValid) // { //iterating through multiple file collection // foreach (HttpPostedFileBase file in files) // { // //Checking file is available to save. // if (file != null) // { // var InputFileName = Path.GetFileName(file.FileName); // var ServerSavePath = Path.Combine(Server.MapPath("~/UploadedFiles/") + InputFileName); // //Save file to server folder // file.SaveAs(ServerSavePath); // //assigning file uploaded status to ViewBag for showing message to user. // ViewBag.UploadStatus = files.Count().ToString() + " files uploaded successfully."; // } // } // } //} if (files != null) { foreach (var file in files) { // extract only the filename var fileName = Path.GetFileName(file.FileName); // extract the file content to byte array var content = new byte[file.ContentLength]; // reads the content from stream file.InputStream.Read(content, 0, file.ContentLength); //get file extesion var fileExtension = Path.GetExtension(fileName); //save file name as uniqe var uniqueFileName = Guid.NewGuid().ToString(); ChildrenFileUpload objChildrenFileUpload = new ChildrenFileUpload { File_Name = uniqueFileName, Upload_Date = DateTime.Now, File_Content = content }; db.ChildrenFileUploads.Add(objChildrenFileUpload); db.SaveChanges(); } } return(RedirectToAction("ChildrenLists")); } catch (Exception) { throw; } }