List <SimpleCMS.Models.File> AddFiles(IEnumerable <FileUploadInfo> fileUploadInfo) { var files = new List <SimpleCMS.Models.File>(); if (fileUploadInfo == null) { return(files); } byte[] data = null; FileInfo fileInfo = null; foreach (var item in fileUploadInfo) { ReadFileData(item, out data, out fileInfo); if (data != null && fileInfo != null) { var file = new SimpleCMS.Models.File() { Name = fileInfo.Name, ContentType = MimeMapping.GetMimeMapping(fileInfo.FullName), Length = fileInfo.Length, Data = data, ChangeEvent = CreateChangeEvent() }; files.Add(file); } } return(files); }
public virtual ActionResult Upload(string path, HttpPostedFileBase file) { path = NormalizePath(path); var fileName = Path.GetFileName(file.FileName); if (AuthorizeUpload(path, file)) { var filePath = Path.Combine(Server.MapPath(path), fileName); file.SaveAs(filePath); var fileInfo = new FileInfo(filePath); //var bytes = System.Text.Encoding.Default.GetBytes(filein ); var bytes = System.IO.File.ReadAllBytes(filePath); var imageInfo = new ImageInfo() { Width = -1, Height = -1, Description = string.Empty, ChangeEvent = CreateChangeEvent() }; using (FileStream fs = new FileStream(Server.MapPath(Path.Combine("~/SimpleCMS/Data/Images", fileInfo.Name)), FileMode.Open, FileAccess.Read)) { using (var img = System.Drawing.Image.FromStream(stream: fs, useEmbeddedColorManagement: false, validateImageData: false)) { float width = img.PhysicalDimension.Width; float height = img.PhysicalDimension.Height; //float hresolution = img.HorizontalResolution; //float vresolution = img.VerticalResolution; imageInfo.Width = (int)width; imageInfo.Height = (int)height; } fs.Close(); } var f = new SimpleCMS.Models.File() { Name = fileInfo.Name, Length = fileInfo.Length, Data = bytes, ContentType = MimeMapping.GetMimeMapping(fileInfo.FullName), ChangeEvent = CreateChangeEvent() }; db.Files.Add(f); db.Images.Add(imageInfo); db.SaveChanges(); return(Json(new { size = file.ContentLength, name = fileName, type = "f" }, "text/plain")); } throw new HttpException(403, "Forbidden"); }
internal List <ImageInfo> SaveImages() { var images = new List <ImageInfo>(); //var files = AddFiles(vmObj.Files); foreach (var fileInfo in FileUploadHandler.UploadedFiles) { var file = new SimpleCMS.Models.File() { Name = fileInfo.Name, Length = fileInfo.Length, ContentType = MimeMapping.GetMimeMapping(fileInfo.FullName), Data = System.IO.File.ReadAllBytes(Server.MapPath(Path.Combine(GetImagesPath(), fileInfo.Name))), ChangeEvent = CreateChangeEvent() }; var imageInfo = new ImageInfo() { Width = -1, Height = -1, Description = string.Empty, File = file, ChangeEvent = CreateChangeEvent() }; using (FileStream fs = new FileStream(Server.MapPath(Path.Combine(GetImagesPath(), file.Name)), FileMode.Open, FileAccess.Read)) { using (Image img = Image.FromStream(stream: fs, useEmbeddedColorManagement: false, validateImageData: false)) { float width = img.PhysicalDimension.Width; float height = img.PhysicalDimension.Height; //float hresolution = img.HorizontalResolution; //float vresolution = img.VerticalResolution; imageInfo.Width = (int)width; imageInfo.Height = (int)height; } fs.Close(); } db.Files.Add(file); db.Images.Add(imageInfo); images.Add(imageInfo); //try //{ // db.SaveChanges(); // images.Add(imageInfo); //} //catch (Exception ex) //{ // // TODO: Handle exception //} } return(images); }
public ActionResult SaveFile(IEnumerable <HttpPostedFileBase> Files) { var fh = new FileUploadHandler(Server, Session, false, "~/SimpleCMS/Data/Images"); if (fh.Save(Files)) { //var files = AddFiles(vmObj.Files); foreach (var fileInfo in FileUploadHandler.UploadedFiles) { var file = new SimpleCMS.Models.File() { Name = fileInfo.Name, Length = fileInfo.Length, ContentType = MimeMapping.GetMimeMapping(fileInfo.FullName), Data = System.IO.File.ReadAllBytes(Server.MapPath(Path.Combine("~/SimpleCMS/Data/Images", fileInfo.Name))), ChangeEvent = CreateChangeEvent() }; var dbObj = new ImageInfo() { Width = -1, Height = -1, Description = string.Empty, File = file, ChangeEvent = CreateChangeEvent() }; using (FileStream fs = new FileStream(Server.MapPath(Path.Combine("~/SimpleCMS/Data/Images", file.Name)), FileMode.Open, FileAccess.Read)) { using (Image img = Image.FromStream(stream: fs, useEmbeddedColorManagement: false, validateImageData: false)) { float width = img.PhysicalDimension.Width; float height = img.PhysicalDimension.Height; //float hresolution = img.HorizontalResolution; //float vresolution = img.VerticalResolution; dbObj.Width = (int)width; dbObj.Height = (int)height; } fs.Close(); } db.Files.Add(file); db.Images.Add(dbObj); db.SaveChanges(); } return(Content("")); } return(Content("Error")); }
//[ValidateAntiForgeryToken] public ActionResult AddFileToObject(ViewTemplateViewModel viewTemplate) { if (viewTemplate != null) { var dbObj = db.Set <ViewTemplate>().Where(x => x.Id == viewTemplate.Id).FirstOrDefault(); if (dbObj != null) { var files = AddFiles(viewTemplate.Files); //var file = files.Where(x => !string.IsNullOrEmpty(x.Name)).FirstOrDefault(); //if (file != null) //{ SimpleCMS.Models.File retFile = null; foreach (var file in files) { var viewTemplateFile = new ViewTemplateFile() { ViewTemplate = dbObj, File = file, }; db.Files.Add(file); db.ViewTemplateFiles.Add(viewTemplateFile); dbObj.Files.Add(viewTemplateFile); retFile = file; //} try { db.SaveChanges(); //return new HttpStatusCodeResult(HttpStatusCode.OK); //Response.ContentType = file.ContentType; //Response.Headers.Add("ContentType", file.ContentType); //Response.Headers.Add("ContentLength", "" + file.Length); //return base.File(file.Data, file.ContentType); } catch (Exception) { } } return(Json(retFile)); } } return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); }