在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
ASP.NET的FileUpload控件默认是不支持服务端的onchange事件的,此时可以用一种变通的方法来实现这一功能。 客户端: <asp:FileUpload ID="fuPhoto" onchange="javascript:__doPostBack('lbUploadPhoto','')" runat="server" ToolTip="选择图片" /> <asp:LinkButton ID="lbUploadPhoto" runat="server" OnClick="lbUploadPhoto_Click"></asp:LinkButton> 后台代码: //自动上传事件 protected void lbUploadPhoto_Click(object sender, EventArgs e) { fileUpload(); } //从控件上传文件 public void fileUpload() { if (fuPhoto.PostedFile != null && fuPhoto.PostedFile.ContentLength > 0) { string ext = System.IO.Path.GetExtension(fuPhoto.PostedFile.FileName).ToLower(); if (ext != ".jpg" && ext != ".jepg" && ext != ".bmp" && ext != ".gif") { return; } string filename = "Image_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ext; string path = "./UploadPhoto/" + filename; fuPhoto.PostedFile.SaveAs(Server.MapPath(path)); Response.Redirect("ImageCut.aspx?Picurl=" + Server.UrlEncode(path)); } else { //do some thing; } } |
请发表评论