This way
HTML
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Label ID="lblMessage" runat="server" Text="File uploaded successfully." ForeColor="Green"
Visible="false" />
<asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="Upload" Style="display: none" />
<script type="text/javascript">
function UploadFile(fileUpload) {
if (fileUpload.value != '') {
document.getElementById("<%=btnUpload.ClientID %>").click();
}
}
</script>
Namespace
C#
using System.IO;
VB.Net
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
FileUpload1.Attributes["onchange"] = "UploadFile(this)";
}
protected void Upload(object sender, EventArgs e)
{
FileUpload1.SaveAs(Server.MapPath("~/Uploads/" + Path.GetFileName(FileUpload1.FileName)));
lblMessage.Visible = true;
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
FileUpload1.Attributes("onchange") = "UploadFile(this)"
End Sub
Protected Sub Upload(sender As Object, e As EventArgs)
FileUpload1.SaveAs(Server.MapPath("~/Uploads/" + Path.GetFileName(FileUpload1.FileName)))
lblMessage.Visible = True
End Sub
Screenshot