Asp. NET lets the FileUpload control support browsing Automatic upload function Solution _ practical skill

Source: Internet
Author: User

Asp. NET FileUpload control does not support onchange events on the server side by default, this functionality can be implemented in a flexible way.
This needs to borrow the client's onchange event, call the __doPostBack method to simulate an event triggering process with LinkButton onclick event , the specific code is as follows:

Client:

<asp:fileupload id= "Fuphoto" onchange= javascript:__dopostback (' Lbuploadphoto ', ') "runat=" Server "ToolTip=" Select Picture "/>
<asp:linkbutton id=" Lbuploadphoto "runat=" Server "onclick=" Lbuploadphoto_click "></asp: Linkbutton>

Background code:

//automatic upload event protected void Lbuploadphoto_click (object sender, EventArgs e) {fileupload ()
;
///upload file from Control 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;}} 
Related Article

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.