using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// /// Contains methods to handle files /// public class FileHandler { public FileHandler() { // // TODO: Add constructor logic here // } public static bool CheckUploadedFileExists(string uploadFolderPath, string subDirName, string fileName) { string fullFilePath = ""; if (subDirName.Length > 0) { uploadFolderPath = uploadFolderPath + "\\" + subDirName; } fullFilePath = string.Format(@"{0}\{1}", uploadFolderPath, fileName); if (System.IO.File.Exists(fullFilePath) == true) { return true; } else { return false; } } public static string UploadFile(FileUpload objectFileUpload, bool keepFileName, string subDirName, string addToFileName2, string addToFileName3, string uploadFolderPath, string rootUploadFolder, string allowedExtensions) { string userFileName = ""; string modifiedFileName = ""; string fileExtension = ""; string newFileName = ""; string newFileNamePath = ""; bool uploadAllowed = false; string returnMsg = ""; userFileName = objectFileUpload.PostedFile.FileName; userFileName = userFileName.Substring(userFileName.LastIndexOfAny(new char[] { Char.Parse(@"\"), Char.Parse(@"/") }) + 1); string userFileNameLowerCase = userFileName.ToLower(); if (userFileName != "") { fileExtension = userFileNameLowerCase.Substring(userFileNameLowerCase.LastIndexOfAny(new char[] { Char.Parse(@".") })); // allowedExtensions is a comma-delimited string of allowed extensions // in the form ".xls,.doc,.gif,.jpg,.jpeg" allowedExtensions = Utilities.removeSpaces(allowedExtensions); // Check to see if the extension is permitted if (allowedExtensions.Length > 0) { int i; string[] extensionsAllowed = allowedExtensions.Split(','); for (i = 0; i < extensionsAllowed.Length; i++) { if (fileExtension == extensionsAllowed[i].ToLower()) { uploadAllowed = true; } else { // Format comma-delimited string so it's legible allowedExtensions = Utilities.putSpaceAfterChar(allowedExtensions, ","); returnMsg = "Can't upload " + userFileName + ", only " + allowedExtensions + " files allowed."; } } } } else { returnMsg = "File for upload not specified"; } if (uploadAllowed) { int fileSize = objectFileUpload.PostedFile.ContentLength; returnMsg = ""; if (fileSize > 0) { try { uploadFolderPath = uploadFolderPath + "\\" + subDirName; // Create the dir structure if it doesn't exist if (!System.IO.Directory.Exists(uploadFolderPath)) { System.IO.Directory.CreateDirectory(uploadFolderPath); } System.IO.Directory.SetCurrentDirectory(uploadFolderPath); modifiedFileName = "_" + userFileNameLowerCase; if (!keepFileName) { // We are just keeping the extention modifiedFileName = fileExtension; } // Rename the file for standardization newFileName = string.Format(@"{0}{1}{2}", addToFileName2 + "_", addToFileName3, modifiedFileName); newFileNamePath = string.Format(@"{0}\{1}", uploadFolderPath, newFileName); // If the file already exists, delete it if (System.IO.File.Exists(newFileNamePath) == true) { System.IO.File.Delete(newFileNamePath); } // Save the new file objectFileUpload.PostedFile.SaveAs(newFileNamePath); returnMsg = rootUploadFolder + "/" + subDirName + "/" + newFileName; } catch (Exception ex) { returnMsg = ex.ToString(); } } else { returnMsg = "'" + userFileName + "' contains no data, file NOT uploaded."; } } return returnMsg; } }