public HttpResponseBase upLoadImg() { string json = string.Empty; string errorInfo = string.Empty; VendorBrandSetQuery query = new VendorBrandSetQuery(); query.Brand_Id = uint.Parse(Request.Params["brand_id"]); query.image_sort = 0; query.image_state = 1; query.image_createdate = (uint)BLL.gigade.Common.CommonFunction.GetPHPTime(DateTime.Now.ToString()); _IvendorBrandSet = new VendorBrandSetMgr(connectionString); ImagePathConfig(); string fileName = String.Empty, fileExtention = String.Empty, newFileName = String.Empty, oldFileName = String.Empty; //string localPromoPath = imgLocalPath + vendorPath;//圖片存儲地址 string localPromoPath = imgLocalPath + vendorOriginalPath;//圖片存儲地址 FileManagement fileLoad;//+++++++++++++++++ Boolean sort_repeat = false; for (int i = 0; i < Request.Files.Count; i++) { fileLoad = new FileManagement();//+++++++++++++++ HttpPostedFileBase file = Request.Files[i]; fileName = Path.GetFileName(file.FileName); fileExtention = fileName.Substring(fileName.LastIndexOf(".")).ToLower(); string[] arrary = fileName.Split('_'); if (arrary.Length < 3) { errorInfo += "[" + file.FileName + "] "; errorInfo += Resources.Product.FILE_NAME_ERROR; errorInfo += " 例:brand_1_tupian" + fileExtention; continue; } string brandStr = fileName.Split('_')[0].ToString(); string sortStr = fileName.Split('_')[1].ToString(); string filenameStr = fileName.Split('_')[2].ToString(); Regex reg = new Regex("^?[1-9][0-9]*$"); if (!brandStr.Equals("brand") || !reg.IsMatch(sortStr)) { errorInfo += "[" + file.FileName + "] "; errorInfo += Resources.Product.FILE_NAME_ERROR; errorInfo += " 例:brand_1_tupian" + fileExtention; continue; } query.image_sort = Convert.ToUInt32(sortStr); if (query.image_sort > 255) query.image_sort = 0; //大於數據庫存儲範圍時,默認為0; string newRand = hash.Md5Encrypt(fileLoad.NewFileName(fileName) + DateTime.Now.ToString(), "32"); fileExtention = fileName.Substring(fileName.LastIndexOf(".")).ToLower(); newFileName = newRand + fileExtention; string folder1 = newFileName.Substring(0, 2) + "/";//圖片名前兩碼 string folder2 = newFileName.Substring(2, 2) + "/";//圖片名第三四碼 string ServerPath = string.Empty; FTP f_cf = new FTP(); localPromoPath = imgLocalPath + vendorOriginalPath + folder1 + folder2;//圖片存儲地址 string s = localPromoPath.Substring(0, localPromoPath.Length - (vendorOriginalPath + folder1 + folder2).Length + 1); f_cf.MakeMultiDirectory(s, (vendorOriginalPath + folder1 + folder2).Substring(1, (vendorOriginalPath + folder1 + folder2).Length - 2).Split('/'), ftpuser, ftppwd); ServerPath = Server.MapPath(imgLocalServerPath + vendorOriginalPath + folder1 + folder2); fileName = newFileName; newFileName = localPromoPath + newFileName;//絕對路徑 string ErrorMsg = string.Empty; //錯誤消息 //Resource.CoreMessage = new CoreResource("Vendor");//尋找product.resx中的資源文件 try { bool result = fileLoad.UpLoadFile(file, ServerPath, newFileName, extention, 150, int.Parse(minValue), ref ErrorMsg, ftpuser, ftppwd); if (result)//上傳成功 { //查詢image_sort是否存在重複 if (_IvendorBrandSet.GetSortIsRepeat(query)) { sort_repeat = true; } query.image_filename = fileName; _IvendorBrandSet.SaveBrandStory(query); errorInfo += "[" + file.FileName + "] " + "上傳成功!"; } else { errorInfo += "[" + file.FileName + "] " + ErrorMsg; } } catch (Exception ex) { Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage(); logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message); logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name; log.Error(logMessage); errorInfo += "[" + file.FileName + "] 上傳失敗<br/>"; //json = "{success:false,msg:\"操作失敗,請稍后再試!\"}"; //if (!string.IsNullOrEmpty(errorInfo)) //{ // json = "{success:false,msg:\"<div>操作失敗,請稍后再試!<br/>" + errorInfo + "</div>\"}"; //} } fileName = String.Empty; fileExtention = String.Empty; newFileName = String.Empty; oldFileName = String.Empty;//++++++++++++++++++ localPromoPath = imgLocalPath + vendorOriginalPath;//圖片存儲地址//++++++++++++++++++++ } if (sort_repeat)// json = "{success:true,sort_repeat:true,msg:\"<div>" + errorInfo + "</div>\"}"; else json = "{success:true,sort_repeat:false,msg:\"<div>" + errorInfo + "</div>\"}"; this.Response.Clear(); this.Response.Write(json); this.Response.End(); return this.Response; }
/// <summary> /// 圖檔維護中上傳圖片 /// </summary> /// <returns></returns> public HttpResponseBase UploadPicture() { string json = string.Empty; string errorInfo = string.Empty; VendorBrandSetQuery query = new VendorBrandSetQuery(); query.Brand_Id = uint.Parse(Request.Params["brand_id"]); query.image_sort = 0; query.image_state = 1; query.image_createdate = (uint)BLL.gigade.Common.CommonFunction.GetPHPTime(DateTime.Now.ToString()); _IvendorBrandSet = new VendorBrandSetMgr(connectionString); ImagePathConfig(); string fileName = String.Empty, fileExtention = String.Empty, newFileName = String.Empty, oldFileName = String.Empty; //string localPromoPath = imgLocalPath + vendorPath;//圖片存儲地址 string localPromoPath = imgLocalPath + vendorOriginalPath;//圖片存儲地址 FileManagement fileLoad;//+++++++++++++++++ for (int i = 0; i < Request.Files.Count; i++) { fileLoad = new FileManagement();//+++++++++++++++ HttpPostedFileBase file = Request.Files[i]; fileName = Path.GetFileName(file.FileName); string newRand = hash.Md5Encrypt(fileLoad.NewFileName(fileName) + DateTime.Now.ToString(), "32"); fileExtention = fileName.Substring(fileName.LastIndexOf(".")).ToLower(); newFileName = newRand + fileExtention; string folder1 = newFileName.Substring(0, 2) + "/";//圖片名前兩碼 string folder2 = newFileName.Substring(2, 2) + "/";//圖片名第三四碼 string ServerPath = string.Empty; FTP f_cf = new FTP(); localPromoPath = imgLocalPath + vendorOriginalPath + folder1 + folder2;//圖片存儲地址 string s = localPromoPath.Substring(0, localPromoPath.Length - (vendorOriginalPath + folder1 + folder2).Length + 1); f_cf.MakeMultiDirectory(s, (vendorOriginalPath + folder1 + folder2).Substring(1, (vendorOriginalPath + folder1 + folder2).Length - 2).Split('/'), ftpuser, ftppwd); ServerPath = Server.MapPath(imgLocalServerPath + vendorOriginalPath + folder1 + folder2); fileName = newFileName; newFileName = localPromoPath + newFileName;//絕對路徑 string ErrorMsg = string.Empty; //錯誤消息 Resource.CoreMessage = new CoreResource("Product");//尋找product.resx中的資源文件 try { //bool result = fileLoad.UpLoadFile(file, ServerPath, newFileName, extention, int.Parse(maxValue), int.Parse(minValue), ref ErrorMsg, ftpuser, ftppwd); bool result = fileLoad.UpLoadFile(file, ServerPath, newFileName, extention, 150, int.Parse(minValue), ref ErrorMsg, ftpuser, ftppwd); if (result)//上傳成功 { query.image_filename = fileName; _IvendorBrandSet.SaveBrandStory(query); //json = "{success:true,msg:\"圖片上傳成功!\"}"; } else { errorInfo += "第" + (i + 1) + "張" + ErrorMsg + "<br/>"; //json = "{success:false,msg:\"<div>圖片上傳成功!<br/>但第" + countError + "張" + ErrorMsg + "</div>\"}"; } json = "{success:true,msg:\"圖片上傳成功!\"}"; if (!string.IsNullOrEmpty(errorInfo)) { json = "{success:true,msg:\"<div>操作完成!<br/>但<br/>" + errorInfo + "</div>\"}"; } } catch (Exception ex) { Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage(); logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message); logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name; log.Error(logMessage); json = "{success:false,msg:\"操作失敗,請稍后再試!\"}"; if (!string.IsNullOrEmpty(errorInfo)) { json = "{success:false,msg:\"<div>操作失敗,請稍后再試!<br/>" + errorInfo + "</div>\"}"; } } fileName = String.Empty; fileExtention = String.Empty; newFileName = String.Empty; oldFileName = String.Empty;//++++++++++++++++++ localPromoPath = imgLocalPath + vendorOriginalPath;//圖片存儲地址//++++++++++++++++++++ } this.Response.Clear(); this.Response.Write(json); this.Response.End(); return this.Response; }