Dotnetplace.com

.NET Articles, Web Devploment, ASP.NET, HTML, Sharepoint 2010, Aamir Hasan
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here

Advertise

Loading...


Posted by Aamir Hasan   on Wednesday, September 26, 2012 Total Views:  

This article demonstrates how to upload file using ASP.NET FileUpload control and convert to zip format. Microsoft .NET Frame 4.5 has provided ZipArchiveand ZipArchiveEntry classes. Using zip compression reduces the size of file. Using System.IO.Compression.  Let’s start.

 

Open your Visual Studio 2012, create new web site and select Framework 4.5

I have created new page name as uploadfile.aspx. Add System.IO.Compression reference and add namespace System.IO.Compression and System.IO to this page.

 

Add the following HTML tags on design page.

            <div>

                <asp:FileUpload ID="FileUpload1" runat="server"/>

                <asp:Button ID="Button1" runat="server" Text="Button"

                    OnClick="Button1_Click"/>

                <asp:GridView ID="GridView1" runat="server"></asp:GridView>

                <asp:Label ID="Label1" runat="server"></asp:Label>

            </div>

 

Add the following server side script on code page

    void Upload()

    {

        if (!Directory.Exists(ZipfilePath))

            Directory.CreateDirectory(ZipfilePath);

        if (!Directory.Exists(UploadfilePath))

            Directory.CreateDirectory(UploadfilePath);

        if (FileUpload1.HasFile)

        {

            if (FileUpload1.PostedFile.ContentLength < Maxfilesize)

            {

                try

                {

                    string filename = Path.GetFileName(FileUpload1.FileName);

                    string _path = String.Format("{0}\\{1}", ZipfilePath, filename);

                    FileUpload1.SaveAs(_path);

                    string zipFileName = String.Format("{0}\\{1}_{2}.zip", UploadfilePath, DateTime.Now.ToString("MMddyyhms"), filename);

                    using (ZipArchive newFile = ZipFile.Open(zipFileName, ZipArchiveMode.Create))

                    {

                        newFile.CreateEntryFromFile(_path, filename);

                    }

                    delete(_path);

                    Label1.Text = string.Format("<font color='#080'>Status: File uploaded and zip file created!</font>", zipFileName);

                }

                catch (Exception ex)

                {

                    Label1.Text = string.Format("<font color='#800'>Status: The following error occured: {0}</font>", ex.Message);

                }

            }

        }

    }

 

    public static string ZipfilePath

    {

        get

        {

            return HttpContext.Current.Server.MapPath("~/createzip");

        }

    }

    public static string UploadfilePath

    {

        get

        {

            return HttpContext.Current.Server.MapPath("~/upload");

        }

    }

 

    public static int Maxfilesize

    {

        get

        {

            return 5242880;

        }

    }

    void delete(string file)

    {

        if (File.Exists(file))

            File.Delete(file);

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        Upload();

    }

}

 

 Debug Screen

In Above function when user will select a file and click on upload button. Upload function will be called. In upload function first it will check the createzip and upload folder if both folders does not exist then folders will be created. ASP.NET FileUpload control will upload a file on createzip folder and then ZipArchive class will create a zip file in upload folder and add the uploaded file and delete a file from createzip folder.

 

Output

Download

uploadfile-and-createzip-file.rar (1.27 kb)

Protected by Copyscape Online Plagiarism Tool
Cheap MAC Cosmetics UK
Cheap MAC Cosmetics UK United States
10/27/2014 11:21:40 PM #

I'am impressed, I need to say. Actually rarely do I encounter a blog thats each educative and entertaining, and let me let you know, you have hit the nail on the head. Your idea is excellent; the problem is one thing that not enough individuals are talking intelligently about. I'm very happy that I stumbled across this in my search for one thing regarding this.

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5