This example illustrate how to use Ajax AsycFileUpload Control In Asp.Net to upload filesasynchronously with use of AjaxControlToolkit OnClientUploadComplete, OnUploadedCompleteand OnClientUploadError events.
Download Latest AjaxControlToolkit and put it in Bin folder of your application. register it in html source of page using Register Assembly page directive at the top of page.
Drag and place ToolkitScriptManager, AsyncFileUpload Control and one label from toolbox on page.label will be used to display success or failure message based onOnClientUploadComplete and OnClientUploadError event raised.
We also need to reset default file size limit of 4mb to enable large file uploads.
HTML SOURCE OF PAGE
Register Toolkit
1: <%@ Page Language="C#" AutoEventWireup="true"
2: CodeFile="Default.aspx.cs" Inherits="_Default" %>
3: <%@ Register Assembly="AjaxControlToolkit"
4: Namespace="AjaxControlToolkit"
5: TagPrefix="ajax" %>
1: <head runat="server">
2: <title></title>
3: <script type = "text/javascript">
4: function Success()
5: {
6: document.getElementById("lblMessage").innerHTML = "File Uploaded";
7: }
8:
9: function Error()
10: {
11: document.getElementById("lblMessage").innerHTML = "Upload failed.";
12: }
13: </script>
14: </head>
15: <body>
16: <form id="form1" runat="server">
17: <div>
18: <ajax:ToolkitScriptManager ID="ToolkitScriptManager1"
19: runat="server"/>
20:
21: <ajax:AsyncFileUpload ID="AsyncFileUpload1" runat="server"
22: OnUploadedComplete="SaveUploadedFile"
23: OnClientUploadComplete="Success"
24: UploaderStyle="Modern"
25: OnClientUploadError="Error"
26: ThrobberID="loader"/>
27: <asp:Image ID="loader" runat="server"
28: ImageUrl ="~/Loader.gif"/>
29: <asp:Label ID="lblMessage" runat="server" Text=""/>
30: </form>
31: </body>
C# CODE
1
protected
void
SaveUploadedFile(
object
sender, EventArgs e)
2
{
3
string
uploadedFileName = Path.GetFileName(AsyncFileUpload1.FileName);
4
AsyncFileUpload1.SaveAs(Server.MapPath(
"~/"
) + uploadedFileName);
5
}
VB.NET
1
Protected
Sub
SaveUploadedFile(sender
As
Object
, e
As
EventArgs)
2
Dim
uploadedFileName
As
String
= Path.GetFileName(AsyncFileUpload1.FileName)
3
AsyncFileUpload1.SaveAs(Server.MapPath(
"~/"
) & uploadedFileName)
4
End
Sub
No comments:
Post a Comment