/// <summary> /// Allows the attachment of database records or other types of information which DO NOT require saving to the file system. /// Be aware the distinct displayText class expects the RelatedId and RelatedEnumId to form a unique key. /// </summary> /// <param name="formsRepo"> IFormsRepository necessary for database access.</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></returns> public static int CreateDataAttachment(IFormsRepository formsRepo, int RelatedId, def_FileAttachment fa) { int fileId = -1; Debug.WriteLine("FileUploads CreateDataAttachment FileName: " + fa.FileName); if (!string.IsNullOrEmpty(fa.FilePath)) { def_FileAttachment faQuery = formsRepo.GetFileAttachment(RelatedId, fa.RelatedEnumId, fa.FilePath); if (faQuery == null) { fileId = formsRepo.AddFileAttachment(fa); } else { formsRepo.UpdateFileAttachment(fa); fileId = fa.FileId; } } else { Debug.WriteLine("FileUploads CreateAttachment Error: File not saved."); } return(fileId); }
/// <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="fileStream">input stream containing the contents of the attachment file</param> /// <param name="originalFileName">the original filename of the attachment file</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, Stream fileStream, string originalFileName, string superDir, string subDir, int RelatedId, int RelatedEnumId, int AttachTypeId) { int fileId = -1; if (fileStream != null && !String.IsNullOrEmpty(originalFileName.Trim())) { Debug.WriteLine("FileUploads CreateAttachment FileName: " + originalFileName); // Save the file to the file system. string fileName = SaveUpload(fileStream, Path.GetFileName(originalFileName), subDir, RelatedId, superDir); if (!String.IsNullOrEmpty(fileName)) { // Append the sub directory and the file name if sub directory has a value. String subFileName = (!String.IsNullOrEmpty(subDir)) ? subDir + Path.DirectorySeparatorChar + fileName.Substring(fileName.LastIndexOf(Path.DirectorySeparatorChar) + 1) : fileName.Substring(fileName.LastIndexOf(Path.DirectorySeparatorChar) + 1); def_FileAttachment fa = formsRepo.GetFileAttachment(RelatedId, RelatedEnumId, subFileName); if (fa == null) { fa = new def_FileAttachment(); fa.EnterpriseId = SessionHelper.LoginStatus.EnterpriseID; //fa.GroupId = SessionHelper.LoginStatus.GroupID; //LoginStatus was returning an invalid number for GroupId fa.UserId = SessionHelper.LoginStatus.UserID; fa.AttachTypeId = AttachTypeId; fa.RelatedId = RelatedId; fa.RelatedEnumId = RelatedEnumId; fa.displayText = fileName.Substring(fileName.LastIndexOf(Path.DirectorySeparatorChar) + 1); fa.FilePath = fileName; fa.FileName = subFileName; fa.StatusFlag = "A"; fa.CreatedDate = DateTime.Now; fa.CreatedBy = SessionHelper.LoginStatus.UserID; fileId = formsRepo.AddFileAttachment(fa); } else { fa.FilePath = fileName; fa.FileName = subFileName; fa.StatusFlag = "A"; 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); }
/// <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); }