using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.IO; using OT.Web.Ap_Code; using System.Data; namespace OT.Web { /// /// 文件上傳後臺處理 /// [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class UploadHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Charset = "utf-8"; try { string guid = context.Request.QueryString["guid"]; string folder = context.Request["folder"]; string FileGuid = context.Request["FileGuid"]; if (string.IsNullOrEmpty(FileGuid)) { FileGuid = Guid.NewGuid().ToString(); } string SetCloseImgTypeCode = context.Request["SetCloseImgTypeCode"]; string CloseImgTypeCode = context.Request["CloseImgTypeCode"]; if (SetCloseImgTypeCode == CloseImgTypeCode) { int intSetImgCount = BasePage.gintUploadCloseImgCount; DataSet dsFiles = new OT.BLL.CommonClass().GetFileListBySourceRelationID(FileGuid); if (dsFiles != null && dsFiles.Tables.Count > 0 && dsFiles.Tables[0].Rows.Count >= intSetImgCount) { //超過上傳張數,則返回不上傳 return; } } //LogTextHelper.Info(folder); HttpPostedFile file = context.Request.Files["Filedata"]; if (file != null) { string oldFileName = file.FileName; //原文件名 int size = file.ContentLength; //附件大小 string strTargetRelationID = ""; string newFileName = GetNewFileName(oldFileName, out strTargetRelationID); //生成新文件名(GUID) #region 上傳到遠程伺服器 //FileServerManage fsw = new FileServerManage(); //string uploadFilePath = "/" + newFileName; //if (!string.IsNullOrEmpty(folder)) //{ // uploadFilePath = string.Format("/{0}/{1}", folder, newFileName); //} //bool uploaded = fsw.UploadFile(file.InputStream, "/" + folder + "/" + newFileName); #endregion #region 本地伺服器上傳 //AppConfig config = new AppConfig(); string uploadPath = folder;//config.AppConfigGet("uploadFiles"); if (string.IsNullOrEmpty(uploadPath)) { uploadPath = "uploadFiles"; } uploadPath = Path.Combine(HttpContext.Current.Server.MapPath("/"), uploadPath); uploadPath = Path.Combine(uploadPath, DateTime.Now.ToString("yyyy/MM/dd")); if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } string newFilePath = Path.Combine(uploadPath, newFileName); //LogTextHelper.Info(newFilePath); file.SaveAs(newFilePath); bool uploaded = File.Exists(newFilePath); #endregion if (uploaded) { #region 文件保存成功后,写入附件的数据库记录 OT.BLL.OTB_SYS_Attachments o_SYS_Attachments_BLL = new OT.BLL.OTB_SYS_Attachments(); OT.Model.OTB_SYS_Attachments o_SYS_Attachments_Model = new OT.Model.OTB_SYS_Attachments(); o_SYS_Attachments_Model.TargetRelationID = strTargetRelationID; o_SYS_Attachments_Model.SourceRelationID = FileGuid; o_SYS_Attachments_Model.FileName = oldFileName; o_SYS_Attachments_Model.FilePath = "~/" + folder + "/" + DateTime.Now.ToString("yyyy/MM/dd") + "/" + newFileName; o_SYS_Attachments_Model.SubFileName = System.IO.Path.GetExtension(newFilePath).ToLower(); o_SYS_Attachments_Model.FileSize = size; o_SYS_Attachments_Model.FileType = CloseImgTypeCode; //類別 o_SYS_Attachments_Model.Memo = CloseImgTypeCode != SetCloseImgTypeCode ? "" : file.FileName.Replace(o_SYS_Attachments_Model.SubFileName, ""); //備註 o_SYS_Attachments_Model.IsProtected = "N"; //同意對內授權 o_SYS_Attachments_Model.IsPublic = "N"; //同意對外授權 o_SYS_Attachments_BLL.Add(o_SYS_Attachments_Model); #endregion } context.Response.Write(FileGuid); } else { //LogTextHelper.Error("上傳文件失败"); } } catch (Exception ex) { //LogTextHelper.Error("上傳文件失败", ex); throw; } } /// /// 获取新的名称 比如:aa.jpg转化为aa(20090504).jpg /// /// 文件名称[aa.jpg] /// 新的文件名称 public static string GetNewFileName(string fileName, out string strTargetRelationID) { strTargetRelationID = Guid.NewGuid().ToString(); if (string.IsNullOrEmpty(fileName)) return string.Empty; //文件后缀名 string extenstion = fileName.Substring(fileName.LastIndexOf(".") + 1); string newFileName = strTargetRelationID.Replace("-", "") + "." + extenstion; return newFileName; } public bool IsReusable { get { return false; } } } }