上传图片的方法以及判断
2015-08-08 17:31
387 查看
<asp:FileUpload ID="fulImage" runat="server" Width="346px" /><br />
<asp:Image ID="imgPro" runat="server" Width="118px" Height="88"/><font color="red">(图片大小:300*226px)</font>
<asp:Button ID="btnUpImg" AccessKey="1" runat="server" Text="上传图片" onclick="btnUpImg_Click" Width="96px" Height="30px" />
if (imgPro.ImageUrl.Substring(imgPro.ImageUrl.LastIndexOf("/") + 1).Equals("productDemo.gif"))
strErr.Append("请上传图片!");
string proimg = imgPro.ImageUrl;
<asp:Image ID="imgPro" runat="server" Width="118px" Height="88"/><font color="red">(图片大小:300*226px)</font>
<asp:Button ID="btnUpImg" AccessKey="1" runat="server" Text="上传图片" onclick="btnUpImg_Click" Width="96px" Height="30px" />
if (imgPro.ImageUrl.Substring(imgPro.ImageUrl.LastIndexOf("/") + 1).Equals("productDemo.gif"))
strErr.Append("请上传图片!");
string proimg = imgPro.ImageUrl;
protected void btnUpImg_Click(object sender, EventArgs e) { string picName = ""; string file = "/images/small/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "-" + DateTime.Now.Day + "/"; SetFocus("btnSubmit"); UploadPicture.CreatDirectory(file); txtName.Text = fulImage.FileName.Replace(".gif", "").Replace(".jpg", ""); if (fulImage.FileName != null && fulImage.FileName != "") { picName = UploadPicture.UploadInfo(fulImage, file); if (picName == null) Response.Write("<script>alert('图片上传失败!')</script>"); else imgPro.ImageUrl = UploadPicture.formatPics(picName); } } public static string UploadInfo(FileUpload fu, string file) { Random myRd = new Random(); string prefix = System.IO.Path.GetExtension(fu.FileName).ToLower(); string fileName = ""; if (prefix.Equals(".jpg") || prefix.Equals(".gif") || prefix.Equals(".jpeg")) { string strFileName = DateTime.Now.ToString().Replace("-", "").Replace(":", "").Replace(" ", "") + myRd.Next(1000) + prefix; string name = System.Web.HttpContext.Current.Server.MapPath(file + strFileName); fileName = file + strFileName; fu.SaveAs(name); return fileName; } else return null; }
相关文章推荐
- PageRank及其MapReduce实现
- Mac OS X 10.10中配置Apache + PHP
- django操作mysql时django.db.utils.OperationalError: (2003, "Can't connect to MySQL server")错误的解决办法
- 【codevs1068】乌龟棋noip10年TG----第二个A掉的钻石题
- oracle12创建用户错误ORA-65096: 公用用户名或角色名无效 invalid common user or role name
- 括号配对
- C++设计模式——原型模式
- PhotoShop 图章工具的简单使用
- checkbox的全选与反选
- 阿拉卡拉卡韩国
- A. Toy Cars
- Top 3000+ Python Modules
- C99标准学习笔记(2)——整数提升
- Logistic回归
- ListView性能优化问题总结
- 正则表达式代码生成
- 获取屏幕宽高和密度
- 【原创】文本工具的使用--根据数据库字段快速生成该表对应的Model类属性
- 左右相等问题
- hdu 5363 Key Set 快速幂