How to use AsyncFileUpload in asp.net ajax


AsyncFileUpload in asp.net ajax



AsyncFileUpload is an asp.net ajax control. AsyncFileUpload control allow us to asynchronously upload files
to web server. this control provide a way to check file uploading results both in the server side and client side.




AsyncFileUpload control have the following useful properties those are CompleteBackColor, ContentType, ErrorBackColor,
FileContent, FileName, HasFile, OnClientUploadComplete, OnClientUploadError, OnClientUploadStarted, PostedFile,
ThrobberID, UploaderStyle, UploadingBackColor, and Width.




AsyncFileUpload ajax control CompleteBackColor property value is a color name which color show as AsyncFileUpload
control's background color when upload complete. ErrorBackColor property set a color name which color display as
control's background color when file upload occurs an error. UploadingBackColor property assign a color name which color
show as control's background color when file upload is in progress.




AsyncFileUpload control's ContentType property get the MIME content type of a file which file sent by a client browser.
FileName property get the client uploaded file name. HasFile property indicate whether AsyncFileUpload control contains a file.
FileContent property gets a stream object of client uploaded file. PostedFile property get a HttpPostedFile object that allow access
to the uploaded file.




AsyncFileUpload control's each OnClientUploadStarted, OnClientUploadError and OnClientUploadComplete property set a
javascript function name that can be executed in the client side. OnClientUploadStarted specified javascript function
executed when file uploading started. OnClientUploadError specified function executed when file upload failed. when file upload
complete then the OnClientUploadComplete property specified javascript function executed on client side.




AsyncFileUpload control's UploaderStyle property have two possible values those are Traditional and Modern. this property
change the appearance (look and feel) of AsyncFileUpload control.





AsyncFileUpload control provides two events those are UploadedComplete and UploadedFileError.
AsyncFileUpload control's UploadedFileError event fired when uploaded file is corrupted.
UploadedComplete event fire when file successfully uploaded. both event fired on server side.




AsyncFileUpload asp.net ajax control also provide a method named SaveAs. this method require a string argument which ask
a filename. SaveAs method allow us to save the contents of an uploaded file.





UsingAsyncFileUpload.aspx



<%@ Page Language="C#" AutoEventWireup="true" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!DOCTYPE html>

<script runat="server">
protected void UploadComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
string filePath = Request.PhysicalApplicationPath+ "Image\\" + AsyncFileUpload1.PostedFile.FileName;
AsyncFileUpload1.SaveAs(filePath);
}
</script>
<script type="text/javascript">
function showConfirmation() {
document.getElementById('Label1').innerText = 'upload complete.';
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>How to use AsyncFileUpload in asp.net ajax</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:OrangeRed; font-style:italic;">Ajax AsyncFileUpload Example: How To Use AsyncFileUpload</h2>
<hr width="650" align="left" color="salmon" />
<asp:ScriptManager
ID="ScriptManager1"
runat="server"
>
</asp:ScriptManager>
<cc1:AsyncFileUpload
ID="AsyncFileUpload1"
runat="server"
OnUploadedComplete="UploadComplete"
OnClientUploadComplete="showConfirmation"
BackColor="Pink"
/>
<br />
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
>
</asp:Label>
</div>
</form>
</body>
</html>