public void UploadifyUpload() { HttpContext context = HttpContext.Current; HttpPostedFile files = context.Request.Files["Filedata"]; if (ufh == null) { ufh = new WebHelper.UploadFilesHelper(); } string fileUrl; try { fileUrl = ufh.UploadToTemp(files); } catch { throw; } context.Response.Write(fileUrl); context.Response.End(); }
public void ProcessRequest(HttpContext context) { //context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World"); HttpPostedFile imgFile = context.Request.Files["imgFile"]; if (imgFile == null || imgFile.ContentLength == 0) { showError(context, "请选择文件。"); } string dirName = context.Request.QueryString["dir"]; if (String.IsNullOrEmpty(dirName)) { dirName = "image"; } try { if (ufh == null) { ufh = new WebHelper.UploadFilesHelper(); } string fileUrl = ufh.Upload(imgFile, "HtmlEditorFiles/" + dirName + ""); Hashtable hash = new Hashtable(); hash["error"] = 0; hash["url"] = fileUrl.TrimStart('~'); context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8"); context.Response.Write(JsonMapper.ToJson(hash)); context.ApplicationInstance.CompleteRequest(); } catch (Exception ex) { showError(context, ex.Message); } }
/// <summary> /// 保存数据 /// </summary> private void OnCommit() { #region 获取输入并验证 string sCategoryId = string.Empty; if (ddlCategory.SelectedIndex > 0) { sCategoryId = ddlCategory.SelectedValue; } else { WebHelper.MessageBox.Messager(this.Page, lbtnPostBack, "所属分类为必选项"); return; } string sProductName = txtProductName.Value.Trim(); string sSubtitle = txtSubtitle.Value.Trim(); string sProductPrice = txtPrice.Value.Trim(); decimal productPrice = 0; if (!decimal.TryParse(sProductPrice, out productPrice)) { WebHelper.MessageBox.Messager(this.Page, lbtnPostBack, "价格正确格式为:整数或浮点数"); return; } DateTime dtNow = DateTime.Now; string sPNum = txtPNum.Value.Trim(); string sStockNum = txtStockNum.Value.Trim(); int stockNum = 0; if (!int.TryParse(sStockNum, out stockNum)) { WebHelper.MessageBox.Messager(this.Page, lbtnPostBack, "库存正确格式为:整数"); return; } string sMarketPrice = txtMarketPrice.Value.Trim(); decimal marketPrice = 0; if (!decimal.TryParse(sMarketPrice, out marketPrice)) { WebHelper.MessageBox.Messager(this.Page, lbtnPostBack, "市场价正确格式为:整数或浮点数"); return; } string sPayOptions = txtPayOptions.Value.Trim(); string sCustomAttrs = hCustomAttrs.Value.Trim().Trim('|'); string sDescr = HttpUtility.HtmlDecode(hEditor1.Value); string sHUploadify = hUploadify.Value.Trim().Trim(','); string sImgmain = hPImagMain.Value.Trim(); #endregion if (bll == null) { bll = new BLL.Product(); } Model.Product model = new Model.Product(); string sNewImgmain = sImgmain; string sImagesAppend = string.Empty; //原始图片 string sMainImage = string.Empty; //原始商品主图片 只一个 string sImagesUrl = string.Empty; //产品图片 只一个 string sLImagesUrl = string.Empty; //产品大图 string sMImagesUrl = string.Empty; //产品中图 string sSImagesUrl = string.Empty; //产品小图 string htmlAppend = ""; string errorMsg = string.Empty; if (!string.IsNullOrEmpty(nId)) { #region 修改商品时,对商品图片的操作 if (ViewState["ProductModel"] != null) { WebHelper.UploadFilesHelper ufh = new WebHelper.UploadFilesHelper(); model = (Model.Product)ViewState["ProductModel"]; sMainImage = model.MainImage; sImagesUrl = model.ImagesUrl; sImagesAppend = model.ImagesAppend; sLImagesUrl = model.LImagesUrl; sMImagesUrl = model.MImagesUrl; sSImagesUrl = model.SImagesUrl; List <string> imagesAppends = sImagesAppend.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList <string>(); List <string> lImages = sLImagesUrl.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList <string>(); List <string> mImages = sMImagesUrl.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList <string>(); List <string> sImages = sSImagesUrl.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList <string>(); List <string> newImagesAppends = sHUploadify.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList <string>(); //取交集 List <string> A = newImagesAppends.Intersect(imagesAppends).ToList <string>(); //取差集 List <string> B = newImagesAppends.Except(imagesAppends).ToList <string>(); //取差集 List <string> C = imagesAppends.Except(newImagesAppends).ToList <string>(); try { if (sImgmain != sMainImage) { if (string.IsNullOrEmpty(sImgmain)) { sMainImage = string.Empty; sImagesUrl = string.Empty; } else if (sImgmain.IndexOf("Product") > -1) { sMainImage = sImgmain; sImagesUrl = ufh.GetProductImgMain(sImgmain); } else { string pItemUrl = ufh.FromTempToProduct("~" + sImgmain); htmlAppend += pItemUrl + ","; sNewImgmain = pItemUrl; imagesAppends.Add(pItemUrl); string[] itemthumbnailImages = ufh.GetProductThumbnailImages(pItemUrl); lImages.Add(itemthumbnailImages[1]); mImages.Add(itemthumbnailImages[2]); sImages.Add(itemthumbnailImages[3]); } } foreach (string item in B) { if (item.Trim() != sImgmain) { string pItemUrl = ufh.FromTempToProduct("~" + item); imagesAppends.Add(pItemUrl); string[] itemthumbnailImages = ufh.GetProductThumbnailImages(pItemUrl); lImages.Add(itemthumbnailImages[1]); mImages.Add(itemthumbnailImages[2]); sImages.Add(itemthumbnailImages[3]); } } foreach (string item in C) { imagesAppends.Remove(item); string fileName = VirtualPathUtility.GetFileName(item); string sExtension = VirtualPathUtility.GetExtension(item); string dirName = VirtualPathUtility.GetDirectory(item); string parentPath = dirName + fileName.Replace(sExtension, "") + "/"; lImages.Remove(lImages.Find(delegate(string m) { return(VirtualPathUtility.GetDirectory(m) == parentPath); })); mImages.Remove(lImages.Find(delegate(string m) { return(VirtualPathUtility.GetDirectory(m) == parentPath); })); sImages.Remove(lImages.Find(delegate(string m) { return(VirtualPathUtility.GetDirectory(m) == parentPath); })); ufh.DeleteProductImage("~" + item); } } catch (Exception ex) { errorMsg = ex.Message; } if (B.Count > 0 || C.Count > 0) { sImagesAppend = ""; sLImagesUrl = ""; sMImagesUrl = ""; sSImagesUrl = ""; foreach (string item in imagesAppends) { sImagesAppend += item + ","; htmlAppend += item + ","; } foreach (string item in lImages) { sLImagesUrl += item + ","; } foreach (string item in mImages) { sMImagesUrl += item + ","; } foreach (string item in sImages) { sSImagesUrl += item + ","; } sImagesAppend = sImagesAppend.Trim(','); sLImagesUrl = sLImagesUrl.Trim(','); sMImagesUrl = sMImagesUrl.Trim(','); sSImagesUrl = sSImagesUrl.Trim(','); } } #endregion } else { #region 新增商品时,对商品图片操作 if (!string.IsNullOrEmpty(sHUploadify)) { WebHelper.UploadFilesHelper ufh = new WebHelper.UploadFilesHelper(); string[] items = sHUploadify.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); try { sNewImgmain = ufh.FromTempToProduct("~" + sNewImgmain); string[] thumbnailImages = ufh.GetProductThumbnailImages(sNewImgmain); sMainImage = sNewImgmain; sImagesUrl = thumbnailImages[0]; sLImagesUrl += thumbnailImages[1] + ","; sMImagesUrl += thumbnailImages[2] + ","; sSImagesUrl += thumbnailImages[3] + ","; htmlAppend += sNewImgmain + ","; sImagesAppend += sNewImgmain + ","; foreach (string item in items) { if (item.Trim() != sImgmain) { string pItemUrl = ufh.FromTempToProduct("~" + item); sImagesAppend += pItemUrl + ","; htmlAppend += pItemUrl + ","; string[] itemthumbnailImages = ufh.GetProductThumbnailImages(pItemUrl); sLImagesUrl += itemthumbnailImages[1] + ","; sMImagesUrl += itemthumbnailImages[2] + ","; sSImagesUrl += itemthumbnailImages[3] + ","; } } sImagesAppend = sImagesAppend.Trim(','); htmlAppend = htmlAppend.Trim(','); sLImagesUrl = sLImagesUrl.Trim(','); sMImagesUrl = sMImagesUrl.Trim(','); sSImagesUrl = sSImagesUrl.Trim(','); } catch (Exception ex) { errorMsg = ex.Message; } } #endregion } if (!string.IsNullOrEmpty(errorMsg)) { WebHelper.MessageBox.Messager(this.Page, lbtnPostBack, errorMsg, "系统异常提醒"); return; } hUploadify.Value = htmlAppend; hPImagMain.Value = sNewImgmain; model.CategoryId = sCategoryId; model.ProductName = sProductName; model.Subtitle = sSubtitle; model.ProductPrice = productPrice; model.ImagesUrl = sImagesUrl; model.CreateDate = dtNow; model.PNum = sPNum; model.StockNum = stockNum; model.ImagesAppend = sImagesAppend; model.MainImage = sMainImage; model.LImagesUrl = sLImagesUrl; model.MImagesUrl = sMImagesUrl; model.SImagesUrl = sSImagesUrl; model.MarketPrice = marketPrice; model.PayOptions = sPayOptions; model.CustomAttrs = sCustomAttrs; model.Descr = sDescr; int result = -1; if (!string.IsNullOrEmpty(nId)) { model.ProductId = nId; result = bll.Update(model); } else { model.UserId = userId; result = bll.Insert(model); } if (result == 110) { WebHelper.MessageBox.Messager(this.Page, lbtnPostBack, "已存在相同记录!"); return; } if (result > 0) { WebHelper.MessageBox.MessagerShow(this.Page, lbtnPostBack, "提交成功!", "ListProduct.aspx"); } else { WebHelper.MessageBox.Messager(this.Page, lbtnPostBack, "提交失败,系统异常!", "系统提示"); } }
public void ProcessRequest(HttpContext context) { //context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World"); HttpPostedFile imgFile = context.Request.Files["imgFile"]; if (imgFile == null || imgFile.ContentLength == 0) { showError(context, "请选择文件。"); } string dirName = context.Request.QueryString["dir"]; if (String.IsNullOrEmpty(dirName)) { dirName = "image"; } try { if (ufh == null) { ufh = new WebHelper.UploadFilesHelper(); } string fileUrl = ufh.Upload(imgFile, "HtmlEditorFiles/" + dirName + ""); Hashtable hash = new Hashtable(); hash["error"] = 0; hash["url"] = fileUrl.TrimStart('~'); context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8"); context.Response.Write(JsonMapper.ToJson(hash)); context.ApplicationInstance.CompleteRequest(); } catch (Exception ex) { showError(context, ex.Message); } //String aspxUrl = context.Request.Path.Substring(0, context.Request.Path.LastIndexOf("/") + 1); ////文件保存目录路径 //String savePath = "../attached/"; ////文件保存目录URL //String saveUrl = aspxUrl + "../attached/"; ////定义允许上传的文件扩展名 //Hashtable extTable = new Hashtable(); //extTable.Add("image", "gif,jpg,jpeg,png,bmp"); //extTable.Add("flash", "swf,flv"); //extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb"); //extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2"); ////最大文件大小 //int maxSize = 1000000; //this.context = context; //HttpPostedFile imgFile = context.Request.Files["imgFile"]; //if (imgFile == null) //{ // showError("请选择文件。"); //} //String dirPath = context.Server.MapPath(savePath); //if (!Directory.Exists(dirPath)) //{ // showError("上传目录不存在。"); //} //String dirName = context.Request.QueryString["dir"]; //if (String.IsNullOrEmpty(dirName)) //{ // dirName = "image"; //} //if (!extTable.ContainsKey(dirName)) //{ // showError("目录名不正确。"); //} //String fileName = imgFile.FileName; //String fileExt = Path.GetExtension(fileName).ToLower(); //if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize) //{ // showError("上传文件大小超过限制。"); //} //if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1) //{ // showError("上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。"); //} ////创建文件夹 //dirPath += dirName + "/"; //saveUrl += dirName + "/"; //if (!Directory.Exists(dirPath)) //{ // Directory.CreateDirectory(dirPath); //} //String ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo); //dirPath += ymd + "/"; //saveUrl += ymd + "/"; //if (!Directory.Exists(dirPath)) //{ // Directory.CreateDirectory(dirPath); //} //String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt; //String filePath = dirPath + newFileName; //imgFile.SaveAs(filePath); //String fileUrl = saveUrl + newFileName; //Hashtable hash = new Hashtable(); //hash["error"] = 0; //hash["url"] = fileUrl; //context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8"); //context.Response.Write(JsonMapper.ToJson(hash)); //context.Response.End(); }
/// <summary> /// 批量删除数据 /// </summary> private void OnDelete() { string itemsAppend = hV.Value.Trim(); if (string.IsNullOrEmpty(itemsAppend)) { WebHelper.MessageBox.Messager(this.Page, lbtnPostBack, "请至少勾选一行进行操作", "操作错误", "error"); return; } if (bll == null) { bll = new BLL.Product(); } string[] itemsAppendArr = itemsAppend.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); string pIdAppend = ""; List <string> list = new List <string>(); foreach (string item in itemsAppendArr) { list.Add(item); pIdAppend += string.Format("'{0}',", item); } pIdAppend = pIdAppend.Trim(','); IList <Model.Product> imagesList = bll.GetImagesListInProductIds(pIdAppend); WebHelper.UploadFilesHelper ufh = new WebHelper.UploadFilesHelper(); string errorMsg = ""; try { TransactionOptions options = new TransactionOptions(); options.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted; options.Timeout = TimeSpan.FromSeconds(90); using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options)) { if (bll.DeleteBatch(list)) { if (imagesList != null) { try { foreach (Model.Product model in imagesList) { string sImagesAppend = model.ImagesAppend; string[] images = sImagesAppend.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (string item in images) { ufh.DeleteProductImage(item); } } } catch { } } } //提交事务 scope.Complete(); } } catch (Exception ex) { errorMsg = ex.Message; } if (!string.IsNullOrEmpty(errorMsg)) { WebHelper.MessageBox.Messager(this.Page, lbtnPostBack, errorMsg, "系统异常提示"); return; } WebHelper.MessageBox.MessagerShow(this.Page, lbtnPostBack, "操作成功"); Bind(); }