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

Comments (2) -

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.

Asa Barrick
Asa Barrick United States
4/30/2015 5:21:05 PM #

Thanks for sharing superb informations. Your web-site is so cool. I'm impressed by the details that you have on this website. It reveals how nicely you perceive this subject. Bookmarked this web page, will come back for extra articles. You, my pal, ROCK! I found simply the information I already searched everywhere and just couldn't come across. What a perfect website.

Deena Koshiol
Deena Koshiol United States
6/28/2015 12:22:00 PM #

Great post. I was checking constantly this blog and I am impressed! Extremely helpful information specially the last part Smile I care for such information much. I was looking for this particular information for a long time. Thank you and best of luck.|

Thuy Pujols
Thuy Pujols United States
7/13/2015 2:57:32 AM #

It's really a cool and helpful piece of info. I am happy that you simply shared this useful information with us. Please stay us informed like this. Thank you for sharing.|

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5