Sunday, August 18, 2019

HOW TO UPLOAD FILE AND CREATE ZIP FILE IN ASP.NET USING C#,VB.NET

Description and Implementation: We can create Zip files using DotNetZip Library. For this you need to add the reference of IconicZip.dll in to you website.

upload file and create zip file in asp.net example

Step 1: Download the IconicZip.dll from here and extract the folder. You will find some folders and files in that. 

Step 2: Just open the zip-v1.9 folder and copy the IconicZip.dll file from there and paste in your web site root directory. 

Step 3: Now in the solution explorer right click on your project name and click on Add Reference

Step 4: A new window will appear from where you have to browse and select the location of your IconicZip.dll file and then click on Add button and then Ok button. 

Add two folders in the root directory of your website. Right click on your project name in the solution explore and click Add -> New Folder. Rename this folder to Uploads. Similarly add another folder and name it ZippedFiles.

Uploads folder will have all the uploaded files (if not deleted after zipping).
ZippedFiles folder will have Zipped Files of the uploaded file.

C#.Net Code to upload file and create Zip archives in asp.net
  • In the design page (.aspx) place a FileUpload control to select the file and a Button control to upload the file that you want to Zip.
Source Code:

<fieldset style="width:410px;">
            <legend>Upload and Create Zip file Example in Asp.net</legend>
             <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btnUploadAndZip" runat="server" Text="Upload and Create Zip file" OnClick="btnUploadAndZip_Click"/>
        </fieldset>

Include the following namespaces

using System.IO;
using Ionic.Zip;

and write the following code on Upload and Create Zip file’s  click event in the code behind file(.aspx.cs)

protected void btnUploadAndZip_Click(object sender, EventArgs e)
    {
        string strFilePath = string.Empty;
        try
        {
            if (FileUpload1.HasFile)
            {
                //Get path for saving the uploaded file
                strFilePath = Server.MapPath("~/Uploads/" + FileUpload1.FileName);
                //Saving the file in the server
                FileUpload1.SaveAs(strFilePath);
                //Create the Zip of uploaded file
                using (ZipFile objZip = new ZipFile())
                {
                    //Adding the uploaded file to create zip file
                    objZip.AddFile(strFilePath);
                    //Saving the Zipped file in  ZippedFiles folder in the root directory
                    objZip.Save(Server.MapPath("~/ZippedFiles/DemoZipFile.zip"));
                }
                //If you want to delete the file from Server after finishing the task of createing Zip then
                //write the  code mentioned below otherwise skip this  code.
                File.Delete(strFilePath);
            }
        }
        catch (Exception ex)
        {
            Response.Write("Folllowing Error Ocuured: " + ex.Message.ToString());
        }
        finally
        {
            strFilePath = string.Empty;
        }
    }

VB.Net Code to upload and  create Zip archives in asp.net
  • In the design page (.aspx) place a FileUpload control to select the file and a Button control to upload the file that you want to Zip.
   <fieldset style="width:410px;">
            <legend>Upload and Create Zip file Example in Asp.net</legend>
             <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btnUploadAndZip" runat="server" Text="Upload and Create Zip file" />
        </fieldset>

Import following namespaces

Imports Ionic.Zip
Imports System.IO

and write the following code on Upload and Create Zip file’s  click event in the code behind file(.aspx.vb)

Protected Sub btnUploadAndZip_Click(sender As Object, e As EventArgsHandles btnUploadAndZip.Click
        Dim strFilePath As String = String.Empty
        Try
            If FileUpload1.HasFile Then
                'Get path for saving the uploaded file
                strFilePath = Server.MapPath("~/Uploads/" + FileUpload1.FileName)
                'Saving the file in the server
                FileUpload1.SaveAs(strFilePath)
                'Create the Zip of uploaded file
                Using objZip As New ZipFile()
                    'Adding the uploaded file to create zip file
                    objZip.AddFile(strFilePath)
                    'Saving the Zipped file in  ZippedFiles folder in the root directory
                    objZip.Save(Server.MapPath("~/ZippedFiles/DemoZipFile.zip"))
                End Using
                'If you want to delete the file from Server after finishing the task of createing Zip then
                'write the  code mentioned below otherwise skip this  code.
                File.Delete(strFilePath)
            End If
        Catch ex As Exception
            Response.Write("Folllowing Error Ocuured: " & ex.Message.ToString())
        Finally
            strFilePath = String.Empty
        End Try
    End Sub

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...