/// <summary> /// Adds a new record, or Updates an existing record, to the def_FileAttachment table, then saves the file in the file system. Files in the same directory with the same name /// will be overwritten without warning. Use of sub-directories can prevent this. /// </summary> /// <param name="formsRepo"> IFormsRepository necessary for database access.</param> /// <param name="file">File being uploaded.</param> /// <param name="subDir">Optional superDirectory name, set to null to disable.</param> /// <param name="subDir">Optional subDirectory name, set to null to disable.</param> /// <param name="RelatedId">Identifier used to associate a file with a record type.</param> /// <param name="RelatedEnumId">Type of identifier used in RelatedId.</param> /// <param name="AttachTypeId">Indicates how the file was attached, which may represent the file itself or just a generic type.</param> /// <returns>Int value of the FileId</returns> public static int CreateAttachment(IFormsRepository formsRepo, System.Web.HttpPostedFileWrapper file, string superDir, string subDir, int RelatedId, int RelatedEnumId, int AttachTypeId) { int fileId = -1; if (file != null && !String.IsNullOrEmpty(file.FileName.Trim())) { fileId = CreateAttachment(formsRepo, file.InputStream, file.FileName, superDir, subDir, RelatedId, RelatedEnumId, AttachTypeId); } else { Debug.WriteLine("FileUploads CreateAttachment Error: File not found."); } return(fileId); }
public bool UploadFile(FormCollection formCollection) { bool edit = UAS_Business_Functions.hasPermission(UAS.Business.PermissionConstants.EDIT, UAS.Business.PermissionConstants.ASSMNTS); bool editlocked = UAS_Business_Functions.hasPermission(UAS.Business.PermissionConstants.EDIT_LOCKED, UAS.Business.PermissionConstants.ASSMNTS); int fileId = -1; if (edit || editlocked) { int RelatedEnumId = formsRepo.GetRelatedEnumIdByEnumDescription("formResultId"); int AttachTypeId = formsRepo.GetAttachTypeIdByAttachDescription("Generic Upload"); int formResultId = Convert.ToInt32(formCollection["formResultId"]); System.Web.HttpPostedFileWrapper file = (System.Web.HttpPostedFileWrapper)Request.Files["file" + formResultId.ToString()]; def_FormResults fr = formsRepo.GetFormResultById(formResultId); DateTime now = DateTime.Now; string date = now.Year.ToString() + ((now.Month < 10) ? "0" : "") + now.Month.ToString() + ((now.Day < 10) ? "0" : "") + now.Day.ToString(); string time = ((now.Hour < 10) ? "0" : "") + now.Hour.ToString() + ((now.Minute < 10) ? "0" : "") + now.Minute.ToString() + ((now.Second < 10) ? "0" : "") + now.Second.ToString(); string subDir = date + Path.DirectorySeparatorChar + time; fileId = FileUploads.CreateAttachment(formsRepo, file, null, subDir, formResultId, RelatedEnumId, AttachTypeId); // check if the file uploaded has a duplicated displayText for this assessment. i.e., a logical overwrite. if (fileId > -1) { def_FileAttachment fa = formsRepo.GetFileAttachment(fileId); Dictionary <int, string> texts = FileUploads.RetrieveFileDisplayTextsByRelatedId(formsRepo, formResultId, RelatedEnumId, "A", AttachTypeId); foreach (int k in texts.Keys) { if (texts[k].Equals(fa.displayText) && k != fa.FileId) { FileUploads.DeleteFile(formsRepo, k); } } } } if (fileId > -1) { return(true); } else { return(false); } }
public ActionResult Insert(string name, System.Web.HttpPostedFileWrapper image) { if (!Authentication.IsValid()) { return(Json(new { Error = "Not Authenticated" })); } string base64Image = null; if (image != null) { BinaryReader br = new BinaryReader(image.InputStream); byte[] bytes = br.ReadBytes((Int32)image.InputStream.Length); base64Image = Convert.ToBase64String(bytes); } db.ExecuteCommand($"insert into tbcategories(catname, image)values('{name}', '{base64Image}')"); Category category = CategoryDAO.GetByName(name); return(Json(new { category.Id, Category = CustomHtmlHelper.CustomHtmlHelper.RenderPartialToString("Profile/TableItem/_Category", category, ControllerContext) })); }
public ActionResult Insert(string name, string minified_desc, string desc, System.Web.HttpPostedFileWrapper image, string price, string services) { if (!Authentication.IsValid()) { return(Json(new { Error = "Not Authenticated" })); } string base64Image = null; if (image != null) { BinaryReader br = new BinaryReader(image.InputStream); byte[] bytes = br.ReadBytes((Int32)image.InputStream.Length); base64Image = Convert.ToBase64String(bytes); } PackageDAO.Insert(name, minified_desc, desc, base64Image, price, services); Package p = PackageDAO.GetByName(name); return(Json(new { p.Id, Package = CustomHtmlHelper.CustomHtmlHelper.RenderPartialToString("Profile/TableItem/_Package", p, ControllerContext) })); }
public static string SaveUpload(System.Web.HttpPostedFileWrapper file, string ivIdent, int formResultId) { try { string originalFileName = Path.GetFileName(file.FileName); string fileName = originalFileName.Replace(' ', '_'); string saveDir = System.Configuration.ConfigurationManager.AppSettings["AttachmentDir"] + Path.DirectorySeparatorChar + formResultId + Path.DirectorySeparatorChar + ivIdent; Directory.CreateDirectory(saveDir); string path = Path.Combine(saveDir, fileName); file.SaveAs(path); return(path); } catch (Exception e) { Debug.WriteLine("* * * ERROR FileUploads.SaveUpload: " + e.Message); return(null); } }
/// <summary> /// Overloaded method allowing the creation of the File Attachment record in a higher level method. The FilePath is the only field updated within this method. /// </summary> /// <param name="formsRepo"> IFormsRepository necessary for database access.</param> /// <param name="file">File being uploaded.</param> /// <param name="subDir">Optional superDirectory name, set to null to disable.</param> /// <param name="subDir">Optional subDirectory name, set to null to disable.</param> /// <param name="RelatedId">Identifier used to associate a file with a record type.</param> /// <param name="fa">FileAttachment record to be recorded.</param> /// <returns>Int value of the FileId</returns> public static int CreateAttachment(IFormsRepository formsRepo, System.Web.HttpPostedFileWrapper file, string superDir, string subDir, int RelatedId, def_FileAttachment fa) { int fileId = -1; if (file != null && !String.IsNullOrEmpty(file.FileName.Trim())) { Debug.WriteLine("FileUploads CreateAttachment FileName: " + file.FileName); // Save the file to the file system. string fileName = SaveUpload(file.InputStream, Path.GetFileName(file.FileName), subDir, RelatedId, superDir); if (!string.IsNullOrEmpty(fileName)) { def_FileAttachment faQuery = formsRepo.GetFileAttachment(RelatedId, fa.RelatedEnumId, fileName); if (faQuery == null) { fa.FilePath = fileName; fileId = formsRepo.AddFileAttachment(fa); } else { fa.FilePath = fileName; formsRepo.UpdateFileAttachment(fa); fileId = fa.FileId; } } else { Debug.WriteLine("FileUploads CreateAttachment Error: File not saved."); } } else { Debug.WriteLine("FileUploads CreateAttachment Error: File not found."); } return(fileId); }
public bool UploadCheck(FormCollection formCollection) { bool newFile = true; int RelatedEnumId = formsRepo.GetRelatedEnumIdByEnumDescription("formResultId"); int AttachTypeId = formsRepo.GetAttachTypeIdByAttachDescription("Generic Upload"); string data = Request["arrayData"]; int formResultId = Convert.ToInt32(formCollection["formResultId"]); System.Web.HttpPostedFileWrapper file = (System.Web.HttpPostedFileWrapper)Request.Files["file" + formResultId.ToString()]; def_FormResults fr = formsRepo.GetFormResultById(formResultId); Dictionary <int, string> texts = FileUploads.RetrieveFileDisplayTextsByRelatedId(formsRepo, formResultId, RelatedEnumId, "A", AttachTypeId); foreach (int k in texts.Keys) { if (file.FileName.Equals(texts[k])) { newFile = false; } } return(newFile); }
public ActionResult Insert(string name, string price, string minified_desc, string desc, uint category_id, string time, System.Web.HttpPostedFileWrapper image, string clothing) { if (!Authentication.IsValid()) { return(Json(new { Error = "Not Authenticated" })); } string base64Image = null; if (image != null) { BinaryReader br = new BinaryReader(image.InputStream); byte[] bytes = br.ReadBytes((Int32)image.InputStream.Length); base64Image = Convert.ToBase64String(bytes); } db.ExecuteProcedure("sp_InsertService", name, price, minified_desc, desc, category_id, time, base64Image, clothing); Service s = ServiceDAO.GetByName(name); return(Json(new { s.Id, Service = CustomHtmlHelper.CustomHtmlHelper.RenderPartialToString("Profile/TableItem/_Service", s, ControllerContext) })); }
public ActionResult Insert(string full_name, string username, string email, string password, string number, string birth, string rg, string cpf, string salary, string genre, System.Web.HttpPostedFileWrapper image, string services) { if (!Authentication.IsValid()) { return(Json(new { Error = "Not Authenticated" })); } string base64Image = null; if (image != null) { BinaryReader br = new BinaryReader(image.InputStream); byte[] bytes = br.ReadBytes((Int32)image.InputStream.Length); base64Image = Convert.ToBase64String(bytes); } EmployeeDAO.Insert(full_name, username, email, password, number, birth, rg, cpf, salary, genre, base64Image, services); Employee e = EmployeeDAO.GetByRG(rg); return(Json(new { e.Customer.Account.Id, Employee = CustomHtmlHelper.CustomHtmlHelper.RenderPartialToString("Profile/TableItem/_Employee", e, ControllerContext) })); }