Multiple FileUpload Demo

Vipul
Posted by in ASP.NET category on for Beginner level | Views : 9174 red flag

This is simple demo of fileUpload Controls



Simple copy following code and paste on webform and run.

Multiple File Upload
<%@Page Language="C#"%>
<HTML>
    <HEAD>
        <TITLE>
            Multiple Files Uploading Using C# - Demo
        </TITLE>
    </HEAD>
<BODY>
<script language="C#" runat="Server">
    //Event handler for the upload button
    void UploadFile(object Sender,EventArgs E)
    {
        int IntLoop=0;
        //Iterating through the Request.Files collection
        for(IntLoop=0;IntLoop<Request.Files.Count;IntLoop++)
        {
            if (Request.Files[IntLoop] !=null) //Checking for valid file
            {       
                // Since the FileName gives the entire path we use Substring function to rip of the filename.
                string StrFileName =Request.Files[IntLoop].FileName.Substring(Request.Files[IntLoop].FileName.LastIndexOf("\\") + 1) ;
                string StrFileType = Request.Files[IntLoop].ContentType ;
                int IntFileSize =Request.Files[IntLoop].ContentLength;
                //Checking for the file length. If length is 0 then file is not uploaded.
                if (IntFileSize <=0)
                    Response.Write(" <font color='Red' size='2'>Uploading of file " + StrFileName + " failed. </font><br>");
                else
                {
                    //Saving the file to the web server
                    Request.Files[IntLoop].SaveAs(Server.MapPath(".\\" + StrFileName));
                    Response.Write( "<font color='green' size='2'>Your file " + StrFileName + " of type " + StrFileType + " and size " + IntFileSize.ToString() + " was uploaded successfully.</font><br>");
                }
            }
        }
        Response.Write("<br>Click <a href='MultipleFileUploadDemo.aspx'>here</a> to upload more files");
    }
</script>
<%if(!Page.IsPostBack)
{
%>

<!-- Declaration of server side form.Note the enctype attribute of the form has to be set to multipart/form-data -->
<basefont size="2">
<form id="FrmFileUploadDemo" name="FrmFileUploadDemo"  method="post" enctype="multipart/form-data" runat="server">
    <TABLE align="center"  cellspacing="5">
        <TR>
            <TD>
                <font size="2">Select a file to upload</font> <input type="file" id="File1" name="File1" runat="server">
            </TD>
        <TR>
        <TR>
            <TD>
                <font size="2">Select a file to upload</font> <input type="file" id="File2" name="File2" runat="server">
            </TD>
        <TR>
        <TR>
            <TD>
                <font size="2">Select a file to upload</font> <input type="file" id="File3" name="File3" runat="server">
            </TD>
        <TR>
        <TR>
            <TD align="center">
                <asp:button value="Upload" Text="Upload" runat="server" id="CmdUpload" onClick="UploadFile" />
            </TD>
        </TR>
    </TABLE>
</form>

<%}%>

</font>
</BODY>
</HTML>

Page copy protected against web site content infringement by Copyscape

About the Author

Vipul
Full Name: vipul vk
Member Level: Starter
Member Status: Member
Member Since: 4/15/2009 9:08:15 PM
Country:

http://www.dotnetfunda.com
Working as a software developer

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)