没时间写太多直接上代码
<asp:Image ID="storeIcoUploadImg" runat="server" CssClass="storeIcoUploadImg" ImageUrl="../Photo/default.jpg" /> <asp:FileUpload ID="storeIcoUpload" CssClass="display" οnchange="javascript:__doPostBack('storeIcoUploadBtn','')" runat="server" /> <asp:LinkButton Text="" runat="server" CssClass="display" ID="storeIcoUploadBtn" OnClick="storeIcoUploadBtn_Click" /> <asp:Label Text="" CssClass="display" runat="server" ID="storeIcoUploadFileName" />
ASPX页面Javascript代码/css代码:
$('#storeIcoUploadImg').click(function () { $('#storeIcoUpload').click(); }); .storeIcoUploadImg { width: 100px; height: 100px; border-width: 0px; cursor: pointer; margin: 0px 0px 10px 0px; } .display { display: none; }
ASPX.CS后台代码: <span style="white-space:pre"> </span>//图片服务器WEB地址(个人形象照片、相册) public static string IMAGESERVER_WEBPATH = ConfigurationManager.AppSettings["ImageServerWebPath"]; //图片服务器的物理地址 public static string IMAGESERVER_LOCALPATH = ConfigurationManager.AppSettings["ImageServerLocalPath"];
<span style="white-space:pre"> </span>/// <summary> /// 上传图片限制 /// </summary> public static int mumberMax = 1048567; /// <summary> /// 图片上传格式 /// </summary> public static string[] limitExtension = { ".jpg", ".gif", ".png", "bmp", "jpeg" };
<span style="white-space:pre"> </span>/// <summary> /// 验证图片格式 /// </summary> /// <param name="str"></param> /// <returns></returns> public bool IsImg(string str) { bool isImages = false; //将字符串格式全部转换为小写 string thestr = str.ToLower(); //循环图片格式 for (int i = 0, j = limitExtension.Length; i < j; i++) { if (thestr == limitExtension[i]) { isImages = true; break; } } return isImages; }
<span style="white-space:pre"> </span>/// <summary> /// 文件名称 /// </summary> /// <returns></returns> public string fileName() { return Guid.NewGuid().ToString().Replace("-", ""); }
<span style="white-space:pre"> </span>/// <summary> /// 门店图片上传 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void storeIcoUploadBtn_Click(object sender, EventArgs e) { if (storeIcoUpload.HasFile) { //获取文件扩展名,并转换成小写 string fileExtension = Path.GetExtension(storeIcoUpload.FileName).ToLower(); //验证图片格式 if (IsImg(fileExtension)) { if (storeIcoUpload.PostedFile.ContentLength <= mumberMax) { try { // 文件服务路径 string webPath = WebSite.IMAGESERVER_LOCALPATH + photoPath; if (!Directory.Exists(webPath)) { Directory.CreateDirectory(webPath); } // 文件名 string imgName = fileName(); string storeIcoImgName = imgName + fileExtension; // 服务器上的虚拟路径 string serverPath = webPath + storeIcoImgName; // 保存图片 storeIcoUpload.PostedFile.SaveAs(serverPath); //获取图片 storeIcoUploadImg.ImageUrl = WebSite.IMAGESERVER_WEBPATH + photoPath + storeIcoImgName; storeIcoUploadFileName.Text = storeIcoImgName; } catch (Exception ex) { LogTool.LogWriter.WriteError("名店ICO上传失败!", ex); } } else { Common.MessageBox.Show(this, "上传文件必须小于1M !请重新选择"); } } else { Common.MessageBox.Show(this, "图片类型不对!只支持一下类型: jpg、gif、png、jpeg、bmp "); } } else { Common.MessageBox.Show(this, "请选择需要上传的文件"); } }
<!--图片服务器Web地址--> <add key="ImageServerWebPath" value="http://xxxx.xxxx.xxxx.xxxx:84/File/" /> <!--图片服务器的本地路径--> <add key="ImageServerLocalPath" value="F:\SVN\branch\20160516_3.1_01\WebSites\Manage\File\" />