/// <summary> /// This method saves the image for each click on the «Image_ Dropzone» /// </summary> public PartialViewResult SelectedImageView() { bool isSavedSuccessfully = true; string fName = ""; var image = new Image(); try { foreach (string fileName in Request.Files) { HttpPostedFileBase file = Request.Files[fileName]; //Save file content goes here fName = file.FileName; List <string> extensions = new List <string>() { ".gif", ".png", ".jpg" }; string extension = Path.GetExtension(fName); if (extensions.Contains(extension)) { if (file != null && file.ContentLength > 0) { int size = 2 * 1024 * 1024; if (file.ContentLength <= size) { var originalDirectory = new DirectoryInfo(string.Format("{0}Images\\WallImages", Server.MapPath(@"\"))); string pathString = System.IO.Path.Combine(originalDirectory.ToString(), "imagepath"); var fileName1 = Path.GetFileName(file.FileName); bool isExists = System.IO.Directory.Exists(pathString); if (!isExists) { System.IO.Directory.CreateDirectory(pathString); } var path = string.Format("{0}\\{1}", pathString, file.FileName); file.SaveAs(path); BinaryReader b = new BinaryReader(file.InputStream); byte[] binData = b.ReadBytes((int)file.InputStream.Length); image = new Image { Name = fileName1.Remove(fileName1.Length - 4), Ext = extension.Replace(".", ""), Image1 = binData }; db.Images.Add(image); db.SaveChanges(); Image = image; } else { //ViewBag.Message = "Maximum allowed file size is 2 mb"; } } } else { //ViewBag.Message = "Error. Valid file extensions - '.png', '.jpg', '.gif'"; } } } catch (Exception) { isSavedSuccessfully = false; } if (isSavedSuccessfully) { ViewBag.success = fName; } else { ViewBag.Message = "Error in saving file"; } if (Image != null) { return(PartialView("Partial_Views/_ImageView", !string.IsNullOrEmpty(Image.Name) ? Image : null)); } else { return(PartialView("Partial_Views/_ImageView")); } }
public PartialViewResult SaveUploadedFile() { bool isSavedSuccessfully = true; string fName = ""; try { foreach (string fileName in Request.Files) { HttpPostedFileBase file = Request.Files[fileName]; //Save file content goes here fName = file.FileName; List <string> extensions = new List <string>() { ".gif", ".png", ".jpg", ".txt", ".pdf" }; string extension = Path.GetExtension(fName); if (extensions.Contains(extension)) { if (file != null && file.ContentLength > 0) { int size = 5 * 1024 * 1024; if (file.ContentLength <= size) { var originalDirectory = new DirectoryInfo(string.Format("{0}Images\\WallFiles", Server.MapPath(@"\"))); string pathString = System.IO.Path.Combine(originalDirectory.ToString(), "filepath"); var fileName1 = Path.GetFileName(file.FileName); bool isExists = System.IO.Directory.Exists(pathString); if (!isExists) { System.IO.Directory.CreateDirectory(pathString); } var path = string.Format("{0}\\{1}", pathString, file.FileName); file.SaveAs(path); BinaryReader b = new BinaryReader(file.InputStream); byte[] binData = b.ReadBytes((int)file.InputStream.Length); string name = ""; if (extension.Length == 4) { name = fileName1.Remove(fileName1.Length - 4); } else if (extension.Length == 5) { name = fileName1.Remove(fileName1.Length - 5); } var list = new DataAccess.Models.FileTmp { Name = name, Type = extension.Replace(".", ""), Contents = binData }; db.FilesTmp.Add(list); listFiles.Add(list); db.SaveChanges(); ViewBag.DocumentTypeID = new SelectList(db.DocumentTypes, "DocumentTypeID", "Name"); } else { ViewBag.Message = "Maximum allowed file size is 5 mb"; } } } else { ViewBag.Message = "Error. Valid file extensions - '.png', '.jpg', '.gif', '.pdf','.txt'"; } } } catch (Exception ex) { isSavedSuccessfully = false; } if (isSavedSuccessfully) { ViewBag.success = fName; } else { ViewBag.Message = "Error in saving file"; } return(PartialView("_SaveUploadedFile", listFiles)); }