Asp. NET enables FileUpload controls to support the resolution of the automatic upload feature

Source: Internet
Author: User

Asp. NET fileupload control is not supported by default for server-side onchange events, this can be accomplished in a flexible way.
This requires borrowing the client's onchange event, calling the __doPostBack method to simulate an event-triggering process with the LinkButton onclick event, as follows:

Client: <asp:FileUpload ID= "fuPhoto" onchange= "javascript:__doPostBack(‘lbUploadPhoto‘,‘‘)" runat= "server" ToolTip= "选择图片" /> <asp:LinkButton ID= "lbUploadPhoto" runat= "server" OnClick= "lbUploadPhoto_Click" ></asp:LinkButton> Background code: //自动上传事件 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; } }

Asp. NET enables FileUpload controls to support the resolution of the automatic upload feature

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.