Some File Types pulled from Image field corrupted when using Response.BinaryWrite() to download...

C

Charlie@CBFC

Hi:

I'm uploading documents into a SQL Server Image field and using
Response.BinaryWrite() to download or view them in the browser. Some doc
types like Adobe Illustrator and Photoshop files will open correctly, but
other like Word, Excel and Powerpoint are corrupt. Here is code fragment to
upload:

// Get file in byte array
int fileLen = this.TextBoxFileToUpload.PostedFile.ContentLength;
byte[] arrayFile = new byte [fileLen];
Stream strm = this.TextBoxFileToUpload.PostedFile.InputStream;
string contentType = this.TextBoxFileToUpload.PostedFile.ContentType;
strm.Read(arrayFile, 0, fileLen);

// Run insert commands...

Here is download code fragment:

// Get data into a reader...

// write to browser
Response.ContentType = reader["ContentType"].ToString();
Response.AppendHeader("Content-Disposition", "attachment; filename = " +
fileName);
Response.BinaryWrite((byte[]) reader["FileContents"]);

Should work, seems like I'm missing something.

Thanks,
Charlie
 
A

Arlyn

I am using vb.net and I have got it working uploading and retrieving files
into an image datatype. The only type I havent got to work is pdf.

Here is my code:
' Uploading
fLen = myFile.ContentLength

Dim fBuffer(fLen) As Byte

myFile.InputStream.Read(fBuffer, 0, fLen)

' myFile is of type HttpPostedFile
dr("mcontent") = fBuffer

dr("ilength") = fLen



'Downloading

dr = dt.Rows(0)

Dim fBuffer(dr("ilength")) As Byte

fBuffer = dr("mcontent")

Response.ContentType = dr("cContentType")

'Response.ContentType = "application/octet-stream"

Response.AddHeader("Content-Disposition", _

"attachment; filename=" & dr("cfilename"))

Response.OutputStream.Write(fBuffer, 0, dr("ilength")



I hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top