Monday, 16 April 2012

FILE UPLOAD IN ASP.NET USING C#

The file upload can be performed easily using File upload control in ASP.NET. First include a file upload control, a button, a label to display the upload status and another label to display the uploaded details in the aspx page. Perform the upload operation in the onclick event of the upload button. Validate the file before uploading it in the specified folder. First, check whether the control has the file and check the content length if the file is selected. We need not use the ASP.NET validators to validate the file. Instead I have included labels to display the validated messages. After the file is uploaded, the uploaded details will be displayed in the label as shown in the below example.



In aspx page,


<table cellpadding="4">
   <tr>
       <td >
              <asp:Label ID="UploadLabel" runat="server" Text="Choose a File:" />
       </td>
   </tr>
   <tr>
       <td>
              <asp:FileUpload ID="Upload" runat="server" />
       </td>
   </tr>
   <tr>
       <td>
              <asp:Button ID="UploadButton" runat="server" Text="Upload" Width="200px"
                   onclick="UploadButton_Click" />
       </td>
   </tr>
   <tr>
       <td>
            <asp:Label ID="StatusLabel" runat="server" ForeColor="DarkGreen" />
            <br /><br />
            <asp:Label ID="DetailsLabel" runat="server" ForeColor="DarkGreen" Visible="false"/>
       </td>
    </tr>
</table>


In aspx.cs page


   protected void UploadButton_Click(object sender, EventArgs e)
    {
        if (Upload.HasFile)
        {
            try
            {
                //Check the file size
                if (Upload.PostedFile.ContentLength > 0)
                {
                    //path to which file should be stored
                    string filename = Server.MapPath("~/uploads/" + Upload.FileName);
                    Upload.SaveAs(filename);
                    StatusLabel.Text = "File uploaded successfully!!";
                    // Display the uploaded file's details
                    DetailsLabel.Visible = true;
                    DetailsLabel.Text = string.Format(
                            @"Uploaded file: {0}<br />
                              File size (in bytes): {1:N0}<br />
                              Content-type: {2}",
                              Upload.FileName,
                              Upload.FileBytes.Length,
                              Upload.PostedFile.ContentType);
                }
                else
                {
                    DetailsLabel.Visible = false;
                    StatusLabel.Text = "File could not be uploaded.";
                }
            }
            catch (Exception ex)
            {
                DetailsLabel.Visible = false;
                StatusLabel.Text = "File could not be uploaded. Error Message: " + ex.Message;
            }
        }
        else
        {
            StatusLabel.Text = "Please select a file to upload";
        }
    }

No comments:

Post a Comment