Upload and compress a file in ASP.NET
How to upload a file and compress it using the compression methods in ASP .NET.
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Posted Jun 12, 2011, 11:19 PM EDT
'A String object reads the file name (locally)
Dim strName As String = Path.GetFileName(fileUpload.PostedFile.FileName)
'Create a stream object to read the file.
Dim myStream As Stream = fileUpload.PostedFile.InputStream
'Allocate space in buffer for use according to length of file.
Dim myBuffer(myStream.Length) As Byte
'Read the file using the Stream object and fill the buffer.
myStream.Read(myBuffer, 0, myBuffer.Length)
myStream.Close()
'Change the extension of the file creating a FileStream object.
Dim myCompressedFile As FileStream
myCompressedFile = File.Create( _
Server.MapPath(Path.ChangeExtension(strName, "gz")))
'GZip object that compress the file
Dim myStreamZip As New GZipStream(myCompressedFile, CompressionMode.Compress)
'Write Back
myStreamZip.Write(myBuffer, 0, myBuffer.Length)
myStreamZip.Close()
Me.lblResult.Text = """" + strName lblResult.Text = lblResult.Text + " was compressed &uploaded successfully."
http://www.beansoftware.com/ASP.NET-Tutorials/File-Upload-Compression.aspx
Mayur GujrathiRank #666 · 3.6K reputationPosted Jun 12, 2011, 11:47 PM EDT