Wednesday, February 8, 2012

Display information of uploaded file


Display the information of uploaded file

The following program will display some information of uploaded file. Add the code into any ASP.NET file.
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload" /></div>
    </form>
</body>
</html>

It will create a file upload control and one upload button. Now add the below code within button’s Click event.

protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            try
            {
                FileUpload1.SaveAs("D:\\" + FileUpload1.FileName);
                Response.Write("Filename-"+FileUpload1.FileName+"<br/>");
                Response.Write("File size-" + FileUpload1.PostedFile.ContentLength + "<br/>");
                Response.Write("File Type-" + FileUpload1.PostedFile.ContentType + "<br/>");
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

    }

It will print some information about file ,which is just uploaded in your D drive.

No comments:

Post a Comment