ASP. NET gives the FileUpload control support for browsing automatic uploads

  • 2021-01-03 20:52:45
  • OfStack

ASP. NET's FileUpload control does not support server-side onchange events by default, so there is a workaround for this.
This requires borrowing the client's onchange event and calling the method with EACH ___ 7en to simulate the firing of an event with the OnClick event of LinkButton, as follows:

Client:


<asp:FileUpload ID="fuPhoto" onchange="javascript:__doPostBack('lbUploadPhoto','')" runat="server" ToolTip=" Choose 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 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 articles: