文件接收上传
2015-08-06 13:52
267 查看
protected void Page_Load(object sender, EventArgs e) { HttpPostedFile jpeg_image_upload = Request.Files["Filedata"]; string imageId =DateTime.Now.Ticks.ToString(); string fileName = Path.GetFileName(jpeg_image_upload.FileName); string fileExtension = Path.GetExtension(fileName); string uploadName = imageId + fileExtension; if (jpeg_image_upload.ContentLength > 0) { string imageUrl = Server.MapPath("/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM") + "/" + uploadName); if (!Directory.Exists(Path.GetDirectoryName(imageUrl))) { Directory.CreateDirectory(Path.GetDirectoryName(imageUrl)); } jpeg_image_upload.SaveAs(imageUrl); //保存大图并水印 string bigimg = Server.MapPath("/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM") + "/c" + uploadName); if (!Directory.Exists(Path.GetDirectoryName(bigimg))) { Directory.CreateDirectory(Path.GetDirectoryName(bigimg)); } NCurrtImg.GreateMiniImage(imageUrl, bigimg, 600, 600, "COM");//生成缩略图 //CurrtImg.addWaterMark(tempimg, bigimg, "I");//水印 //保存中图 string midimg = Server.MapPath("/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM") + "/m" + uploadName); if (!Directory.Exists(Path.GetDirectoryName(midimg))) { Directory.CreateDirectory(Path.GetDirectoryName(midimg)); } NCurrtImg.GreateMiniImage(imageUrl, midimg, 200, 200, "COM");//生成缩略图 //保存小图 string smallimg = Server.MapPath("/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM") + "/s" + uploadName); if (!Directory.Exists(Path.GetDirectoryName(smallimg))) { Directory.CreateDirectory(Path.GetDirectoryName(smallimg)); } NCurrtImg.GreateMiniImage(imageUrl, smallimg, 33, 33, "COM");//生成缩略图 Response.StatusCode = 200; string returnStr = jpeg_image_upload.ContentLength.ToString();//0 size returnStr = returnStr + "|" + "/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM") + "/m" + uploadName;//1 url returnStr = returnStr + "|" + fileName; //2 title returnStr = returnStr + "|" + imageId; //3 id returnStr = returnStr + "|" + "wokaokao"; //4 name Response.Write(returnStr); Response.End(); } }
public void UpImg(JTZK_ProductModel Model) { HttpPostedFile file = Request.Files["PicUrl"]; if (file == null) { // return ... } else { string fileName = System.IO.Path.GetFileName(file.FileName); string fileExtension = System.IO.Path.GetExtension(fileName); string uploadName = Guid.NewGuid() + fileExtension; string dir = "/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM"); string imgpath = dir + "/c" + uploadName; Model.PicUrl = imgpath; System.IO.Directory.CreateDirectory(Server.MapPath(dir));//创建文件夹. imgpath = Server.MapPath(imgpath); file.SaveAs(imgpath); string smallimg = ""; string midimg = ""; smallimg = Server.MapPath(dir + "/s" + uploadName); NCurrtImg.GreateMiniImage(imgpath, smallimg, 100, 100, "Cut"); midimg = Server.MapPath(dir + "/m" + uploadName); NCurrtImg.GreateMiniImage(imgpath, midimg, 300, 300, "Cut"); } }
如果失败 考虑权限问题 和路径问题 在 saveAs前一定要创建路径
#region 文件上传 public ActionResult FileUpload() { HttpPostedFileBase postFile = Request.Files["fileUp"];//接收文件数据 if (postFile == null) { return Content("no:请选择上传文件"); } else { string fileName = Path.GetFileName(postFile.FileName);//文件名称 string fileExt = Path.GetExtension(fileName);//文件的扩展名称. if (fileExt == ".jpg") { string dir = "/ImagePath/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/"; Directory.CreateDirectory(Path.GetDirectoryName(Request.MapPath(dir)));//创建文件夹. string newfileName = Guid.NewGuid().ToString();//新的文件名称. string fullDir = dir + newfileName + fileExt;//完整的路径. postFile.SaveAs(Request.MapPath(fullDir));//保存文件. return Content("ok:" + fullDir); } else { return Content("no:文件格式错误!!"); } } } #endregion
相关文章推荐
- Spring学习记录-AOP
- 微软面试、经典算法、编程艺术、红黑树4大系列总结
- 49、android studio 使用技巧记录
- 升级到win10时VirtualBox和ie的两个小问题
- Java 时间格式 new Date()
- 常见的控件(2)
- Android 5.0 双卡短信处理
- GIT 技巧
- Avoiding memory leaks ---- Context Leak 篇
- Netra基于Rdk平台的软件框架设计
- vim配置文件,让编程更随意
- 在world中批量调整图片的大小
- 提高工作效率的TXT转换为PDF方法
- [OpenMP] 怎么在Visual Studio上启用OpenMP
- JDK5新特性:枚举(Enum)
- 3070 Fibonacci 矩阵快速幂
- android 设置字体颜色 颜色大全
- sprintf函数的说明
- 优秀的代码 vs. 糟糕的代码
- 从微软下载安装Windows10