@using (Html.BeginForm("Avatar", "Wap", FormMethod.Post, new { enctype = "multipart/form-data" })) { <div class="tit">修改头像</div> <div class="images_slider_container"> <div class="images_slider"> <ul class="slides"> <li class="clone"> <img src="@Model" alt="" title="" border="0"> </li> </ul> </div> </div> <div class="ui-wrap"> <input type="file" id="image" name="image" value="选择图片"> </div> <input type="submit" value="点击上传形象" class="btn" /> }
/// <summary> /// 上传图片并生成缩略图 /// </summary> /// <param name="upImage">HttpPostedFileBase控件</param> /// <param name="sThumbExtension">缩略图的thumb</param> /// <param name="intThumbWidth">生成缩略图的宽度</param> /// <param name="intThumbHeight">生成缩略图的高度</param> /// <returns>缩略图名称</returns> public string UpLoadImage(HttpPostedFileBase upImage, int intThumbWidth, int intThumbHeight) { string sThumbFile = ""; string sFilename = ""; string sThumbExtension="s"; string sSavePath = "/attached/image/"; string ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo); string timeString = DateTime.Now.ToString("HHmmss_ffff", DateTimeFormatInfo.InvariantInfo); if (upImage.FileName != null) { WeiPay.LogUtil.WriteLog("!!!!!!!!!!!!!!!!!!!文件名" + upImage.FileName); int nFileLen = upImage.ContentLength; if (nFileLen == 0) return "没有选择上传图片"; if(nFileLen > 1000 * 1000) return "文件大于1M,不能上传!"; //获取upImage选择文件的扩展名 string extendName = System.IO.Path.GetExtension(upImage.FileName).ToLower(); WeiPay.LogUtil.WriteLog("!!!!!!!!!!!!!!!!!!!选择文件的扩展名" + extendName); //判断是否为图片格式 if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png") return "图片格式不正确"; WeiPay.LogUtil.WriteLog("!!!!!!!!!!!!!!!!!!!图片格式不正确"); byte[] myData = new Byte[nFileLen]; upImage.InputStream.Read(myData, 0, nFileLen); //sFilename = System.IO.Path.GetFileName(upImage.FileName); //int file_append = 0; //检查当前文件夹下是否有同名图片,有则在文件名+1 //while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename))) //{ // file_append++; // sFilename = System.IO.Path.GetFileNameWithoutExtension(upImage.FileName) // + file_append.ToString() + extendName; //} sSavePath += ymd + "/"; if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath))) { Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(sSavePath)); } sFilename = timeString + extendName; System.IO.FileStream newFile = new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename), System.IO.FileMode.Create, System.IO.FileAccess.Write); newFile.Write(myData, 0, myData.Length); newFile.Close(); WeiPay.LogUtil.WriteLog("!!!!!!!!!!!!!!!!!!!以上为上传原图"); //以上为上传原图 try { //原图加载 using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename))) { //原图宽度和高度 int width = sourceImage.Width; int height = sourceImage.Height; int smallWidth; int smallHeight; //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽 和 原图的高/缩略图的高) if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight) { smallWidth = intThumbWidth; smallHeight = intThumbWidth * height / width; } else { smallWidth = intThumbHeight * width / height; smallHeight = intThumbHeight; } //判断缩略图在当前文件夹下是否同名称文件存在 //file_append = 0; //sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(upImage.FileName) + extendName; //while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile))) //{ // file_append++; // sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(upImage.FileName) + // file_append.ToString() + extendName; //} sThumbFile = timeString + sThumbExtension + extendName; //缩略图保存的绝对路径 string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(sSavePath) + sThumbFile; //新建一个图板,以最小等比例压缩大小绘制原图 using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight)) { //绘制中间图 using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) { //高清,平滑 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.Black); g.DrawImage( sourceImage, new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight), new System.Drawing.Rectangle(0, 0, width, height), System.Drawing.GraphicsUnit.Pixel ); } //新建一个图板,以缩略图大小绘制中间图 using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight)) { //绘制缩略图 using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1)) { //高清,平滑 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.Black); int lwidth = (smallWidth - intThumbWidth) / 2; int bheight = (smallHeight - intThumbHeight) / 2; g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel); g.Dispose(); bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg); } } } } } catch { //出错则删除 System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)); return "图片格式不正确"; } //返回缩略图名称 return sSavePath + sThumbFile; } return "没有选择图片"; }
/// <summary> /// 修改头像 /// </summary> /// <returns></returns> [Authorize] public ActionResult Avatar() { var mb = memberservice.GetMemberInfo(WebSecurity.CurrentUserName); return View(model: mb.member_avatar); } [HttpPost] public ActionResult Avatar(HttpPostedFileBase image) { string strimg = wapService.UpLoadImage(image, 112, 120); if (strimg.IndexOf("attached") != -1) { return Content("<script>alert('" + strimg + "');</script>"); } else { memberservice.setAvatar(WebSecurity.CurrentUserName, strimg); return Content("<script>alert('您已成功修改您的会员头像!');</script>"); } }
本地测试在上传图片时有时候正常,但有的图片在点击上传是不会直接执行后台代码
我看你1M就不能上传了,图片这样设计不合理把
提交后代码都没有反应,有什么合理不合理的?
@Socials: 为什么代码没有反应?你先去看看你的表单提交后的请求是什么,参数是什么,粗看没什么问题,你的enctype也是正确的
@稳稳的河: 就是应为enctype的问题,如果我去掉enctype就有反应,但是如果加上enctype就没有反应,但是不加enctype的话HttpPostedFileBase控件就是错误的
没有上传过。。。来学习一下