public UploadUtility(StoreAreaForUpload area, CustomFileType fileType = CustomFileType.Image) { _siteConfig = new CustomSiteConfig(fileType); switch (area) { case (StoreAreaForUpload.ForVideo): { _siteConfig.AttachPath = _siteConfig.AttachPath + "/Videos"; _siteConfig.AttachImgMaxWidth = 180; _siteConfig.AttachImgMaxHeight = 101; break; } case (StoreAreaForUpload.ForGallery): { _siteConfig.AttachPath += "/Gallery"; break; } case (StoreAreaForUpload.ForBook): { _siteConfig.AttachPath += "/Books"; _siteConfig.ThumbnailWidth = 250; _siteConfig.ThumbnailHeight = 360; _siteConfig.AttachImgMaxWidth = 500; _siteConfig.AttachImgMaxHeight = 720; break; } case (StoreAreaForUpload.ForArticle): { _siteConfig.AttachPath += "/Articles"; _siteConfig.AttachImgMaxWidth = 500; _siteConfig.AttachImgMaxHeight = 280; break; } case (StoreAreaForUpload.ForNews): { _siteConfig.AttachPath += "/News"; _siteConfig.AttachImgMaxWidth = 400; _siteConfig.AttachImgMaxHeight = 300; break; } case (StoreAreaForUpload.ForFeedback): { _siteConfig.AttachPath += "/Feedback"; _siteConfig.AttachSave = 3; break; } } }
/// <summary> /// 初始化CustomSiteConfig类,默认上传的是图片类型 /// </summary> /// <param name="fileType"></param> public CustomSiteConfig(CustomFileType fileType) { WebPath = "/"; if (fileType == CustomFileType.Image) { AttachPath = "Images"; AttachSave = 1; AttachExtension = "gif,jpg,png,bmp"; AttachFileSize = 51200; /*图片格式*/ AttachImgSize = 10240; AttachImgMaxHeight = 800; AttachImgMaxWidth = 800; //缩略图所需图片大小 ThumbnailHeight = 300; ThumbnailWidth = 300; ThumbFormationType = "Cut"; //以下为水印所需 WatermarkType = 1; //1为文字,2为图片 WatermarkImgQuality = 80; WatermarkPic = "/Content/qs_watermark.png"; WatermarkText = "求索工作室"; WatermarkPosition = 9; WatermarkFont = "Tahoma"; WatermarkFontSize = 12; WatermarkTransparency = 5; } if (fileType == CustomFileType.Media) { AttachPath = "Attached"; AttachSave = 1; AttachExtension = "gif,jpg,png,bmp,rar,zip,doc,xls,txt"; AttachFileSize = 51200; } if (fileType != CustomFileType.File) { return; } AttachPath = "Profiles"; AttachExtension = "rar,zip,doc"; AttachFileSize = 51200; }
public bool UploadFileOnPath(HttpPostedFileBase file, CustomFileType FileType) { bool result = false; try { if (file.ContentLength > 0) { SurrogateService surrogateService = new SurrogateService(); Documents documents = new Documents(); string _FileName = Path.GetFileName(file.FileName); string _FileType = string.Empty; string _NewFileName = string.Empty; string _RootPath = Server.MapPath(@"~/UploadedFiles/" + ApplicationManager.LoggedInUser.UserID.ToString()); if (!System.IO.File.Exists(_RootPath)) { Directory.CreateDirectory(_RootPath); } string _path = Path.Combine(_RootPath, _FileName); file.SaveAs(_path); _FileType = Path.GetExtension(Path.Combine(_RootPath, _FileName)); _NewFileName = _FileName + "_" + Path.GetRandomFileName() + _FileType; System.IO.File.Move(Path.Combine(_RootPath, _FileName), Path.Combine(_RootPath, _NewFileName)); documents.UserID = ApplicationManager.LoggedInUser.UserID.ToString(); documents.IDProof = _NewFileName; documents.UploadPath = _RootPath; documents.EntityState = EntityState.Save; documents.ChangeBy = ApplicationManager.LoggedInUser.UserID.ToString(); documents = surrogateService.SaveDocuments(documents); switch (FileType) { case CustomFileType.IdProof: ApplicationManager.UploadedIDProof = _NewFileName; break; case CustomFileType.Pic: ApplicationManager.UploadedPic = _NewFileName; break; case CustomFileType.FamilyPic: ApplicationManager.UploadedFamilyPic = _NewFileName; break; } } WebHelper.SetMessageAlertProperties(this, ResponseType.Success.ToString(), "Document uploaded Successfully!", "5000"); WebHelper.SetMessageBoxProperties(this, ResponseType.Success, "Document uploaded Successfully!"); result = true; } catch (Exception ex) { LoggerHelper.WriteToLog(ex); result = false; } return(result); }
//XML Parser for CustomFileTypes.xml private static List <CustomFileType> ReadCustomFileTypeInstructions(string file) { var list = new List <CustomFileType>(); XDocument doc = XDocument.Load(file); foreach (var node in doc.Root.Elements()) { if (node.Name == "CustomFileType") { CustomFileType type = new CustomFileType(); foreach (var el in node.Elements()) { if (!el.IsEmpty) { switch (el.Name.ToString()) { case "Filename": if (el.HasAttributes) { try { type.contains = bool.Parse(el.Attribute("contains").Value); } catch { Debug.Log("Error parsing filename of custom file type, should only have 'contains' attribute with boolean value"); } } type.filename = el.Value; break; case "HandlerType": type.typename = el.Value; break; case "HandlerMethod": type.methodname = el.Value; break; default: break; } } } if (type.filename != "") { if (type.methodname != "" && type.typename != "") { Debug.Log("Listed Type for Handler for files '" + type.filename + "' is " + type.typename); Debug.Log("Listed Method for Handler for files '" + type.filename + "' is " + type.methodname); list.Add(type); } else { Debug.LogError(file + " : Invalid Type"); } } else { Debug.LogError(file + " : " + node.Name + " : Empty filename"); } } } return(list); }