protected void lnkSetDefault_Click(object sender, EventArgs e) { int imgid = CommonMethod.ConvertToInt(hidId.Value, 0); string filename = string.Empty; string physicalpath = string.Empty; string thumpath = string.Empty; if (imgid > 0) { XiHuan_GoodsImageEntity defaultimage = new XiHuan_GoodsImageEntity(); defaultimage.Id = imgid; defaultimage.Retrieve(); if (defaultimage.IsPersistent) { physicalpath = Server.MapPath(defaultimage.ImgSrc); filename = Path.GetFileNameWithoutExtension(physicalpath); thumpath = physicalpath.Replace(filename, filename + GlobalVar.DefaultPhotoSize); if (!File.Exists(physicalpath))//没有缩略图,需要生成缩略图 { PicHelper.MakeThumbnail(physicalpath, thumpath, 85, 85); thumpath = physicalpath.Replace(filename, filename + GlobalVar.BigPhotoSize); PicHelper.MakeThumbnail(physicalpath, thumpath, 200, 220); } Query.ProcessSqlNonQuery(@"update XiHuan_GoodsImage set IsDefaultPhoto=0 where IsDefaultPhoto=1 and GoodsId=" + Request["id"] + ";update XiHuan_UserGoods set DefaultPhoto='" + defaultimage.ImgSrc.Replace(filename, filename + GlobalVar.DefaultPhotoSize) + "' where Id=" + Request["id"], GlobalVar.DataBase_Name); defaultimage.IsDefaultPhoto = 1; defaultimage.Save(); CommonMethod.readAspxAndWriteHtmlSoruce("showdetail.aspx?id=" + defaultimage.GoodsId, CommonMethod.FinalString(Request["detailurl"])); Alert("恭喜:成功设置为默认图片!"); BindReceive(); } } }
protected void btnSubmit_Click(object sender, EventArgs e) { if (!IsUserAlreadyLogin) { MemberCenterPageRedirect("", "goodsadd.aspx" + (IsEdit ? "?id=" + CommonMethod.FinalString(Request["id"]) : "")); } else { #region 务器端验证 以后完善 #endregion #region 保存换品信息 Transaction t = new Transaction(); XiHuan_UserGoodsEntity newgoods = null; if (IsEdit) { newgoods = XiHuan_UserGoodsEntityAction.RetrieveAXiHuan_UserGoodsEntity(CommonMethod.ConvertToInt(Request["id"], 0)); } else { newgoods = new XiHuan_UserGoodsEntity(); } newgoods.OwnerId = CurrentUserId; newgoods.OwnerName = CurrentUserName; newgoods.Name = txtGoodName.Text.Trim(); newgoods.IsTJ = chkTJ.Checked ? (byte)1 : (byte)0; newgoods.TypeId = CommonMethod.ConvertToInt(ddlGoodType.SelectedValue, 0); newgoods.ChildId = CommonMethod.ConvertToInt(ddlGoodChildType.SelectedValue, 0); newgoods.IsHavePhoto = rbtYes.Checked ? (byte)XiHuan_UserGoodsFacade.IsGoodHavePhoto. : (byte)XiHuan_UserGoodsFacade.IsGoodHavePhoto.无; newgoods.Description = txtGoodDesc.Value.Trim(); newgoods.NewDeep = byte.Parse(ddlNewOldDeep.SelectedValue.Trim()); newgoods.OnlyCityChange = chkValidCity.Checked ? (byte)1 : (byte)0; newgoods.OnlySchoolChange = chkValidSchool.Checked ? (byte)1 : (byte)0; newgoods.HopeToChangeTypeId = CommonMethod.ConvertToInt(ddlGoodType1.SelectedValue, 0); newgoods.HopeToChangeChildTypeId = CommonMethod.ConvertToInt(ddlGoodChildType1.SelectedValue, 0); newgoods.HopeToChangeDesc = txtHopeToChangeDesc.Text.Trim(); newgoods.ProvinceId = CurrentUser.ProvinceId; newgoods.ProvinceName = CurrentUser.ProvinceName; newgoods.CityId = CurrentUser.CityId; newgoods.CityName = CurrentUser.CityName; newgoods.AreaId = CurrentUser.AreaId; newgoods.AreaName = CurrentUser.AreaName; newgoods.SchoolId = CurrentUser.SchoolId; newgoods.SchoolName = CurrentUser.SchoolName; if (!IsEdit) { newgoods.CreateDate = DateTime.Now; newgoods.ViewCount = new Random().Next(10, 30); newgoods.GoodState = (byte)XiHuan_UserGoodsFacade.GoodsState.新登记; } newgoods.IsChecked = (byte)(SystemConfigFacade.Instance().IsGoodsAddNeedCheck ? 0 : 1); t.DoSaveObject(newgoods); if (!IsEdit) { #region 换品图片上传 string gooddefaultphoto = string.Empty; if (rbtYes.Checked) { string extention = string.Empty; int filesize = 0; string filepath = string.Empty; string savepath = string.Empty; string filename = string.Empty; HttpFileCollection goodimages = HttpContext.Current.Request.Files; for (int i = 0; i < goodimages.Count; i++) { HttpPostedFile currentfile = goodimages[i]; extention = Path.GetExtension(currentfile.FileName); filesize = currentfile.ContentLength; filepath = "images/userupload/goodsimage/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/"; //不符合条件的直接跳过,不进行保存 if (!(currentfile.FileName.Length > 0) || filesize == 0 || !CommonMethod.IsUploadImageValid("", extention) || filesize > 500 * 1024) { continue; } else { if (!Directory.Exists(Server.MapPath(filepath))) { Directory.CreateDirectory(Server.MapPath(filepath)); } filename = newgoods.Id.ToString() + "_" + DateTime.Now.ToString("yyyyMMddhhmmssfff"); currentfile.SaveAs(Server.MapPath(filepath + filename + extention)); XiHuan_GoodsImageEntity newgoodimage = new XiHuan_GoodsImageEntity(); newgoodimage.GoodsId = newgoods.Id; newgoodimage.GoodsName = newgoods.Name; newgoodimage.ImgSrc = filepath + filename + extention; newgoodimage.CreateDate = DateTime.Now; if (i == 0) { gooddefaultphoto = filepath + filename + GlobalVar.DefaultPhotoSize + extention; newgoodimage.IsDefaultPhoto = 1; //生成不同规格的图片 PicHelper.MakeThumbnail(Server.MapPath(filepath + filename + extention), Server.MapPath(filepath + filename + GlobalVar.DefaultPhotoSize + extention), 85, 85); PicHelper.MakeThumbnail(Server.MapPath(filepath + filename + extention), Server.MapPath(filepath + filename + GlobalVar.BigPhotoSize + extention), 200, 220); } t.DoSaveObject(newgoodimage); } } } #endregion #region 默认图片保存 string sql = string.Format("update XiHuan_UserGoods set DefaultPhoto='{0}' where Id={1} ", gooddefaultphoto.Length > 0 ? gooddefaultphoto : "images/none.jpg", newgoods.Id); t.DoSqlNonQueryString(sql, GlobalVar.DataBase_Name); #endregion #region 更新用户换品数量和积分,换币 string updategoodsnumber = string.Format(@"update XiHuan_UserInfo set GoodsNumber=GoodsNumber+1, Score=Score+{0},HuanBi=HuanBi+{1} where Id={2}" , SystemConfigFacade.Instance().AddScoreByAddGoods(), SystemConfigFacade.Instance().AddHBByAddGoods(), CurrentUser.ID); t.DoSqlNonQueryString(updategoodsnumber, GlobalVar.DataBase_Name); #endregion #region 浏览人 XiHuan_GoodsViewUserEntity view = new XiHuan_GoodsViewUserEntity(); view.GoodsId = newgoods.Id; view.Type = 0; view.VisitDate = DateTime.Now; view.VisitorName = "喜换网"; view.VisitorId = 1; view.VisitorHeadImage = "images/userupload/20092113032102_1.png"; t.DoSaveObject(view); #endregion } try { string detailurl = "goods/" + newgoods.CreateDate.Year + "/" + newgoods.CreateDate.Month + "/" + newgoods.CreateDate.Day + "/goods" + newgoods.Id + ".html"; t.DoSqlNonQueryString("update XiHuan_UserGoods set DetailUrl='" + detailurl + "' where Id=" + newgoods.Id, GlobalVar.DataBase_Name); t.Commit(); if (!SystemConfigFacade.Instance().IsGoodsAddNeedCheck) { DataTable dt = Query.ProcessSql("select Id,DetailUrl,GoodState from XiHuan_UserGoods with(nolock) where OwnerId= " + CurrentUserId + " and IsChecked=1 ", GlobalVar.DataBase_Name); foreach (DataRow dr in dt.Rows) { CommonMethod.readAspxAndWriteHtmlSoruce("showdetail.aspx?id=" + dr["Id"], dr["DetailUrl"].ToString()); } } if (IsEdit) { Alert("恭喜:换品信息保存成功" + (SystemConfigFacade.Instance().IsGoodsAddNeedCheck ? ",我们会尽快进行审核" : string.Empty) + "^_^!"); } else { Alert("恭喜:换品登记成功" + (SystemConfigFacade.Instance().IsGoodsAddNeedCheck ? ",我们会尽快进行审核" : string.Empty) + "^_^!"); SendMailFacade.sendEmail("[email protected],[email protected]", "有人在喜换网发换品了", "有人在喜换网发换品:" + txtGoodName.Text); } ExecScript("window.location='goodlist.aspx?s='+Math.random();"); } catch (Exception ex) { t.RollBack(); Alert("抱歉:换品保存出错," + ex.Message); return; } #endregion } }
protected void btnUpLoad_Click(object sender, EventArgs e) { string action = CommonMethod.FinalString(Request["action"]); if (action.Equals("addNewPic") && !flpImage.HasFile) { Alert("您没有选择要上传的图片!"); return; } else { XiHuan_GoodsImageEntity newgoodimage = null; if (action.Equals("addNewPic")) { newgoodimage = new XiHuan_GoodsImageEntity(); } if (action.Equals("ModifyPic")) //删除旧图片 { newgoodimage = XiHuan_GoodsImageEntityAction.RetrieveAXiHuan_GoodsImageEntity(CommonMethod.ConvertToInt(Request["id"], 0)); } newgoodimage.GoodsId = CommonMethod.ConvertToInt(Request["gid"], 0); newgoodimage.GoodsName = Microsoft.JScript.GlobalObject.unescape(CommonMethod.FinalString(Request["gname"])); newgoodimage.ImgDesc = txtImgDesc.Text.Trim(); newgoodimage.CreateDate = DateTime.Now; newgoodimage.IsDefaultPhoto = (byte)(chkIsDefault.Checked ? 1 : 0); if (flpImage.HasFile) { if (chkIsDefault.Checked)//把原来的默认图片更新 { Query.ProcessSqlNonQuery("update XiHuan_GoodsImage set IsDefaultPhoto=0 where IsDefaultPhoto=1 and GoodsId=" + CommonMethod.FinalString(Request["gid"]), GlobalVar.DataBase_Name); } string extention = Path.GetExtension(flpImage.FileName); int filesize = flpImage.PostedFile.ContentLength; string filepath = "images/userupload/goodsimage/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/"; if (!CommonMethod.IsUploadImageValid("", extention)) { Alert("您选择的图片格式不正确!"); return; } if (filesize > 500 * 1024) { Alert("您选择的图片太大了,超过500k了,换个小点的吧!"); return; } if (!Directory.Exists(Server.MapPath(filepath))) { Directory.CreateDirectory(Server.MapPath(filepath)); } filepath += CommonMethod.FinalString(Request["gid"]) + "_" + DateTime.Now.ToString("yyyyMMddhhmmssfff") + extention; flpImage.PostedFile.SaveAs(Server.MapPath(filepath)); if (action.Equals("ModifyPic")) { string imgsrc = CommonMethod.FinalString(newgoodimage.ImgSrc); if (imgsrc.Length > 0 && File.Exists(Server.MapPath(imgsrc)) && !imgsrc.Equals("images/none.jpg")) { File.Delete(Server.MapPath(imgsrc)); //如果有缩略图也要删除掉 string imgname = Path.GetFileNameWithoutExtension(Server.MapPath(imgsrc)); imgsrc = Server.MapPath(newgoodimage.ImgSrc.Replace(imgname, imgname + GlobalVar.DefaultPhotoSize)); if (File.Exists(imgsrc)) { File.Delete(imgsrc); } imgsrc = Server.MapPath(newgoodimage.ImgSrc.Replace(imgname, imgname + GlobalVar.BigPhotoSize)); if (File.Exists(imgsrc)) { File.Delete(imgsrc); } } } newgoodimage.ImgSrc = filepath; } newgoodimage.Save(); if (chkIsDefault.Checked) //更改默认图片 { string filename = Path.GetFileNameWithoutExtension(Server.MapPath(newgoodimage.ImgSrc)); //生成缩略图 PicHelper.MakeThumbnail(Server.MapPath(newgoodimage.ImgSrc), Server.MapPath(newgoodimage.ImgSrc.Replace(filename, filename + GlobalVar.DefaultPhotoSize)), 85, 85); PicHelper.MakeThumbnail(Server.MapPath(newgoodimage.ImgSrc), Server.MapPath(newgoodimage.ImgSrc.Replace(filename, filename + GlobalVar.BigPhotoSize)), 200, 220); Query.ProcessSqlNonQuery(string.Format("update XiHuan_UserGoods set IsHavePhoto=1,DefaultPhoto='{0}' where Id={1} ", newgoodimage.ImgSrc.Replace(filename, filename + GlobalVar.DefaultPhotoSize), newgoodimage.GoodsId), GlobalVar.DataBase_Name); } Alert("恭喜:操作成功!"); ExecScript(string.Format("parent.location='modifypic.aspx?id={0}&name={1}'", CommonMethod.FinalString(Request["gid"]), Server.UrlEncode(CommonMethod.FinalString(Request["gname"])))); } }
protected void btnDealGoodsImage_Click(object sender, EventArgs e) { string opath = string.Empty; string thumpath = string.Empty; string despath = string.Empty; string filename = string.Empty; string filepath = string.Empty; string fixpath = "images/userupload/goodsimage/"; string extention = string.Empty; System.Text.StringBuilder sb = new System.Text.StringBuilder(); #region 默认图片的处理 RetrieveCriteria rcGooos = new RetrieveCriteria(typeof(XiHuan_UserGoodsEntity)); Condition c = rcGooos.GetNewCondition(); if (txtGoodsId.Text.Trim().Length > 0) { c.AddEqualTo(XiHuan_UserGoodsEntity.__ID, txtGoodsId.Text); } rcGooos.AddSelect(XiHuan_UserGoodsEntity.__ID); rcGooos.AddSelect(XiHuan_UserGoodsEntity.__DEFAULTPHOTO); rcGooos.OrderBy(XiHuan_UserGoodsEntity.__ID, false); c.AddNotEqualTo(XiHuan_UserGoodsEntity.__DEFAULTPHOTO, "images/none.jpg"); EntityContainer goodscontainer = rcGooos.AsEntityContainer(); foreach (XiHuan_UserGoodsEntity goods in goodscontainer) { if (goods.DefaultPhoto != null && goods.DefaultPhoto.Length > 0) { opath = Server.MapPath(goods.DefaultPhoto); filename = Path.GetFileNameWithoutExtension(opath); extention = Path.GetExtension(opath); if (File.Exists(opath)) { filepath = fixpath + goods.CreateDate.Year + "/" + goods.CreateDate.Month + "/" + goods.CreateDate.Day + "/"; despath = Server.MapPath(filepath + filename + extention); if (!Directory.Exists(Server.MapPath(filepath))) { Directory.CreateDirectory(Server.MapPath(filepath)); } if (!File.Exists(despath)) { File.Copy(opath, despath, true);//复制原来的图片到新的路径 //File.Delete(opath); } thumpath = Server.MapPath(filepath + filename + GlobalVar.DefaultPhotoSize + extention); PicHelper.MakeThumbnail(despath, thumpath, 85, 85); thumpath = Server.MapPath(filepath + filename + GlobalVar.BigPhotoSize + extention); PicHelper.MakeThumbnail(despath, thumpath, 200, 220); sb.Append("Update XiHuan_UserGoods set DefaultPhoto='" + filepath + filename + GlobalVar.DefaultPhotoSize + extention + "' WHERE Id=" + goods.Id + ";"); } } } #endregion if (sb.ToString().Length > 0) { Query.ProcessSqlNonQuery(sb.ToString(), GlobalVar.DataBase_Name); } }
public async Task <List <Dictionary <string, string> > > ImgUpload() { if (!Request.Content.IsMimeMultipartContent("form-data")) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } List <Dictionary <string, string> > list = new List <Dictionary <string, string> >(); string pata = "/File/uploadpic/" + DateTime.Now.ToString("yyyyMMdd"); LogHelper.WriteLog("pata : " + pata, "UTIController.FileUpload"); string root = HttpContext.Current.Server.MapPath(pata + "/original");//指定要将文件存入的服务器物理位置 LogHelper.WriteLog("root: " + root, "UTIController.FileUpload"); if (!Directory.Exists(root))//如果不存在就创建file文件夹 { Directory.CreateDirectory(root); } var provider = new MultipartFormDataMemoryStreamProvider(); try { // Read the form data. await Request.Content.ReadAsMultipartAsync(provider); LogHelper.WriteLog("Read the form data", "UTIController.FileUpload"); // This illustrates how to get the file names. foreach (var fileContent in provider.FileContents) { LogHelper.WriteLog("This illustrates how to get the file names.", "UTIController.FileUpload"); Dictionary <string, string> dic = new Dictionary <string, string>(); string fileName = fileContent.Headers.ContentDisposition.FileName.Replace("\"", ""); dic.Add("OldFileName", fileName); string Ext = Path.GetExtension(fileName).ToLowerInvariant(); if (Ext == ".png" || Ext == ".jpg" || Ext == ".jpeg") { dic.Add("ExtName", Ext); fileName = EncyryptionUtil.GetMd5Utf8(EncyryptionUtil.AESEncrypt(Guid.NewGuid().ToString())) + Ext; dic.Add("NewFileName", fileName); dic.Add("FilePath", FlieServiceUrl + pata + "/original/" + fileName); var stream = await fileContent.ReadAsStreamAsync(); using (StreamWriter sw = new StreamWriter(Path.Combine(root, fileName))) { dic.Add("status", "1"); stream.CopyTo(sw.BaseStream); sw.Flush(); } PicHelper.PicZoomAuto(HttpContext.Current.Server.MapPath(pata + "/original/" + fileName), HttpContext.Current.Server.MapPath(pata), fileName); } else { dic.Add("status", "0"); dic.Add("errmsg", "格式不正确"); } list.Add(dic); } //TODO:这样做直接就将文件存到了指定目录下,暂时不知道如何实现只接收文件数据流但并不保存至服务器的目录下,由开发自行指定如何存储,比如通过服务存到图片服务器 //foreach (var key in provider.FormData.AllKeys) //{//接收FormData // dic.Add(key, provider.FormData[key]); //} } catch (Exception e) { LogHelper.WriteLog(e.Message); throw; } return(list); }