how to upload multiple images in asp.net

Description
Here i will explain how to upload multiple files in asp.net c#. Also you can display upload progress and status of uploading files. It is simple way to use AjaxFileUpload control for fulfill your all requirement following below.

First of all you need to add below line on top to head part in  your .aspx page :


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

After this, put below ScriptManager in .aspx page:


<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

Now add AjaxFileUpload control in your .aspx page like below:


<asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server"
 AllowedFileTypes="jpg,jpeg,png,gif"  MaximumNumberOfFiles="20"
 OnUploadComplete="File_Upload" Width="50%" />

Note: OnUploadComplete method name as your code behind method.

After adding control, code below in .aspx.cs page to save selected file in your folder
like below:


protected void File_Upload(object sender, AjaxFileUploadEventArgs e)
{
   string filename = System.DateTime.Now.ToFileTime().ToString();
   string destinationPath = Server.MapPath("Images/" + filename + ".jpg");
   AjaxFileUpload1.SaveAs(@destinationPath);
}

No comments:

Post a Comment