From: Hemant on
Hi,

I am working on VS 2005.
I want to upload and download office 2007 document .
below is the code giving warning whlie downloading the file.
warning is saying of corruption of content . But if i click on yes it shows
the file content.
I don't want this warning message.
Please help me.
I am downloading and uploading data in chunks.

Private Sub SaveWithEncrypted()

If System.IO.File.Exists(Server.MapPath("~") + "/test/test1.docx") Then

File.Delete(Server.MapPath("~") + "/test/test1.docx")

End If

Dim theStream As System.IO.Stream

theStream = fileuploader.FileContent

Dim UE As New UnicodeEncoding

Dim des As New RijndaelManaged

Dim key As Byte()

key = UE.GetBytes("encrypts")

Dim myicryptotransform As ICryptoTransform = des.CreateEncryptor(key, key)

Dim fileData(BLOCKSIZE - 1) As Byte

Dim fs As FileStream = New FileStream(Server.MapPath("~") +
"/test/test1.docx", FileMode.CreateNew)

Dim mycryptostream As New CryptoStream(fs, myicryptotransform,
CryptoStreamMode.Write)

Dim intLastBlockNum As Integer = Math.Truncate((theStream.Length) /
BLOCKSIZE)

Dim intLastBlockSize As Integer

Math.DivRem(CInt(theStream.Length - 1), BLOCKSIZE, intLastBlockSize)

For intBlock As Integer = 0 To intLastBlockNum

theStream.Read(fileData, 0, _

IIf(intBlock = intLastBlockNum, intLastBlockSize, BLOCKSIZE))

mycryptostream.Write(fileData, 0, _

IIf(intBlock = intLastBlockNum, intLastBlockSize, BLOCKSIZE))

Next

mycryptostream.Close()

fs.Close()


End Sub

Private Sub GetWithEncrypted()

If System.IO.File.Exists(Server.MapPath("~") + "/test/test1.docx") Then

Dim UE As New UnicodeEncoding

Dim des As New RijndaelManaged

Dim key As Byte() = UE.GetBytes("encrypts")

Dim iFileLength As Integer

Response.Clear()

Response.ClearHeaders()

Response.ContentType =
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"

Response.AppendHeader("Content-disposition", "attachment;
filename=document.docx")

Dim myFilestream As New FileStream(Server.MapPath("~") + "/test/test1.docx",
FileMode.Open, FileAccess.Read)

Dim myicryptotransform As ICryptoTransform = des.CreateDecryptor(key, key)

Dim mycryptostream As New CryptoStream(myFilestream, myicryptotransform,
CryptoStreamMode.Read)

iFileLength = myFilestream.Length

Dim myoutputdata(BLOCKSIZE) As Byte

Dim intLastBlockNum As Integer = Math.Truncate(iFileLength / BLOCKSIZE)

Dim intLastBlockSize As Integer

Math.DivRem(CInt(iFileLength), BLOCKSIZE, intLastBlockSize)

For intBlock As Integer = 0 To intLastBlockNum

If intBlock = intLastBlockNum Then ReDim myoutputdata(intLastBlockSize)

mycryptostream.Read(myoutputdata, 0, _

IIf(intBlock = intLastBlockNum, intLastBlockSize, BLOCKSIZE))

Response.BinaryWrite(myoutputdata)

Next

mycryptostream.Close()

myFilestream.Close()


End If

End Sub





Thanks,

Hemant