In this article I will explain with an example, how to display images after upload using AJAX Control Toolkit AsyncFileUpload control without page refresh or PostBack in ASP.Net using C# and VB.Net.
Using the ASP.Net AJAX Control Toolkit
Registering ASP.Net AJAX Control Toolkit
In order to use ASP.Net AJAX Control Toolkit controls, you will need to add reference of AJAX Control Toolkit Library and then register on the Page as shown below.
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
HTML Markup
The following HTML Markup consists of:
ScriptManager – For enabling
AJAX.
AsyncFileUpload – For uploading image file.
Events
OnClientUploadComplete – For triggering the
JavaScript uploadComplete function. It used when the file is successfully uploaded.
OnClientUploadStarted – For triggering the
JavaScript uploadStarted function. It displays the image after each time a new file is uploaded.
OnUploadedComplete – For triggering the Server Side FileUploadComplete event. It stores the uploaded file in specified location.
Image – For displaying Loader (Animated) image.
img – For displaying uploaded image preview.
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<cc1:AsyncFileUpload ID="AsyncFileUpload1" runat="server" Width="400px" UploaderStyle="Modern"
OnClientUploadComplete="uploadComplete"CompleteBackColor="White" UploadingBackColor="#CCFFFF"
ThrobberID="imgLoader" OnUploadedComplete="FileUploadComplete" OnClientUploadStarted="uploadStarted" />
<asp:Image ID="imgLoader" runat="server" ImageUrl="~/images/loader.gif" /><br />
<br />
<img id="imgDisplay" src="" alt="" style="display: none" />
Displaying images after upload without page refresh or postback
uploadStarted
Inside the
uploadStarted JavaScript function, the HTML image element is referenced and made hidden when a new file is selected.
uploadComplete
Inside the
uploadComplete JavaScript function, the uploaded image is referenced and image location is stored.
Then, the img.onload function is called and CSS is set.
Finally, the name of uploaded file is referenced using
get_filename method of the ASP.Net
AJAX Control Toolkit AsyncFileUpload Control along with folder path of the uploaded file is set to the image
SRC attribute.
<script type="text/javascript">
function uploadStarted() {
$get("imgDisplay").style.display = "none";
}
function uploadComplete(sender, args) {
var imgDisplay = $get("imgDisplay");
imgDisplay.src = "images/loader.gif";
imgDisplay.style.cssText = "";
var img = new Image();
img.onload = function () {
imgDisplay.style.cssText = "height:100px;width:100px";
imgDisplay.src = img.src;
};
img.src = "<%=ResolveUrl(UploadFolderPath) %>" + args.get_fileName();
}
</script>
Namespaces
You will need to import the following namespace.
C#
VB.Net
Uploading file to Folder (Directory)
First, a string variable is created with the location of the Folder (Directory) where the images will be saved.
Inside the FileUploadComplete event handler, the name of the file is referenced using GetFileName method of Path class.
Finally, the file is saved in the Folder (Directory) using
SaveAs method of
AJAX Control Toolkit AsyncFileUpload.
C#
protected string UploadFolderPath = "~/Uploads/";
protected void FileUploadComplete(object sender, EventArgs e)
{
string filename = Path.GetFileName(AsyncFileUpload1.FileName);
AsyncFileUpload1.SaveAs(Server.MapPath(this.UploadFolderPath) + filename);
}
VB.Net
Protected UploadFolderPath As String = "~/Uploads/"
Protected Sub FileUploadComplete(ByVal sender As Object, ByVal e As EventArgs)
Dim filename As String = Path.GetFileName(AsyncFileUpload1.FileName)
AsyncFileUpload1.SaveAs(Server.MapPath(Me.UploadFolderPath) & filename)
End Sub
Screenshot
Downloads