Enable the fileupload control of ASP. NET to support automatic browsing and uploading.

Source: Internet
Author: User

By default, the fileupload control does not support onchange events on the server side.

The onchange event of the client is used to call the _ dopostback method to use The onclick event of linkbutton to simulate an event triggering process.CodeAs follows:

Client:

<Asp: fileupload id = "fuphoto" onchange = "javascript :__ dopostback ('lbuploadphoto ','')"
Runat = "server" tooltip = "select image"/>

<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 a file from the 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.