Wednesday 1 May 2013

Upload and Extract file in Asp.net C3#

Upload and Extract file in ASP.NET C#


.aspx

<div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btnUpload" runat="server" Text="Upload and Extract"
            onclick="btnUpload_Click" />
        <br />
        <asp:Label ID="lblMsg" runat="server"></asp:Label>
    </div>


.cs

 if (FileUpload1.HasFile)
        {
            string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            FileUpload1.PostedFile.SaveAs(Server.MapPath("compressed") + "/" + FileName);

            FileInfo finfo = new FileInfo(Server.MapPath("compressed") + "/" + FileName);
            long FileInBytes = finfo.Length;
            long FileInKB = finfo.Length / 1024;
            FastZip archiveFile = new FastZip();
            archiveFile.ExtractZip(Server.MapPath("compressed") + "/" + FileName, Server.MapPath("extracted"), "");
            lblMsg.Text = "File uploaded and extracted successfully,,<br />";
            lblMsg.Text += "File Name: " + FileName + "<br />";
            lblMsg.Text += "Size:" + FileInKB.ToString() + "KB <br />";
        }