Upload and Compressed file


By using the code you can upload file and then compressed it. It Retrieve file information and upload to server.

Upload and Compressed file



Description



By using the code you can upload file and then compressed it.
It Retrieve file information and upload to server.



Add below code in HTML page



<form id="form1" runat="server">
<table>
<tr>
<td>
<input id="fileUpload" type="file" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtZipFileName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtSource" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtDestination" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Button ID="Button2" runat="server" Text="Upload & Compressed" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblResult" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</form>


Add below Namespaces




Imports System
Imports System.IO
Imports System.IO.Compression
Imports System.Diagnostics


Add below code in Codebehind on Button1 click






If fileUpload.PostedFile Is Nothing Then
Me.lblResult.Text = "No File Selected to Upload."
Exit Sub
End If


'Retrieve file information and upload to server.
Dim strName As String
strName = System.IO.Path.GetFileName(fileUpload.PostedFile.FileName)

Try
fileUpload.PostedFile.SaveAs(Server.MapPath(strName))
Me.lblResult.Text = """" + strName + """ was uploaded successfully."
Catch ex As Exception
Me.lblResult.Text = "An Error Occured While Uploading File."
End Try



Add below code in Codebehind on Button2 click






If txtZipFileName.Text.Trim() = "" Then
lblResult.Text = "You must enter what you want the name of the zip file to be"
'Change the background color to cue the user to what needs fixed
'txtZipFileName.BackColor = Color.Yellow
Exit Sub
Else
'Reset the background color
'txtZipFileName.BackColor = Color.White
End If
'Launch the zip.exe console app to do the actual zipping
Dim i As New System.Diagnostics.ProcessStartInfo(AppDomain.CurrentDomain.BaseDirectory & "zip.exe")
i.CreateNoWindow = True
Dim args As String = ""
If txtSource.Text.IndexOf(" ") <> -1 Then
'we got a space in the path so wrap it in double qoutes
args += """" & txtSource.Text & """"
Else
args += txtSource.Text
End If

Dim dest As String = txtDestination.Text
If dest.EndsWith("\") = False Then
dest += "\"
End If
'Make sure the zip file name ends with a zip extension
If txtZipFileName.Text.ToUpper().EndsWith(".ZIP") = False Then
txtZipFileName.Text += ".zip"
End If
dest += txtZipFileName.Text
If dest.IndexOf(" ") <> -1 Then
'we got a space in the path so wrap it in double qoutes
args += (" " & """") + dest & """"
Else
args += " " & dest
End If
i.Arguments = args

'Mark the process window as hidden so that the progress copy window doesn't show
i.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
Dim p As System.Diagnostics.Process = System.Diagnostics.Process.Start(i)
p.WaitForExit()
Response.Write("Complete")


Related Articles

Working with FileUpload Control

This will show you how to access FileUpload properties programmatically and how to save a file selected by FileUpload control.

More articles: FileUpload

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: