DOCX FILE CORRUPTED WHEN RETRIEVED FROM SQL2005

S

SUNNY

Hi, i am uploading a .docx file into sql2005 and later when i retrieve the
file from the database and i open it, i get a error message "The file is
corrupted and cannot be open". I am not facing this issue when i store and
retrieve files of type .doc

i am storing the file in a column of datatype image.
Here is the code where i insert the file into database. Am sure the code is
working.
Am using VB Script here.

Set dbRS = server.createObject("ADODB.Recordset")
dbRS.Open "attachment", dbConn, 3, 3
dbRS.AddNew
dbRS.Fields("attachment_name").Value = File.FileName
dbRS.Fields("attachment_size").Value = File.FileSize
dbRS.Fields("attachment_mime").Value = File.ContentType
dbRS.Fields("id").Value = request.querystring("id")
dbRS.Fields("feedback_attachment_data").AppendChunk File.FileData & ChrB(0)

dbRS.Update
dbRS.Close


Here is the code where i retrieve the file from the database.

set dbRS = dbConn.execute("SELECT attachment_name, attachment_mime,
attachment_data FROM attachments WHERE (attachment_id = " &
Request.QueryString("id") & ")")

If Not dbRS.EOF Then
Response.ContentType = dbRS("attachment_mime")
Response.AddHeader "Content-Disposition", "attachment;filename=" &
dbRS("attachment_name")
Response.BinaryWrite dbRS("attachment_data")
Else
Response.Write("File could not be found")
End If

Please let me know as to why i am not able to retrieve the .docx files
properly.
 
C

CLY

Hello, I have the same issue and do not see a solution on the Internet. I
know that code which produces this error results in a database-stored file
that is typically uploaded to the server with an extra byte at the end.
However, in this case, if the results are similar to mine, then the file is
getting stored with a byte removed (at the very least, the stored file size
is not the same as the original file size, which seems to be the real issue).
Also, in my cases, for some reason, all of my Office 2007 files that were
tested have an original file size that ends in an odd number (10861), and
when I store these files to the server, using the code below and similar
code, the process seems forced to store a file whose size ends in an even
number (i.e. 10860, 10862) - thus not the same size as the original.

If anyone can confirm these results or post an alogorithm which uses
AppendChunk to store an Office 2007 file successfully...thanks in advance!
 
J

Judith Pl

Hi,
I had the same problem - byte array size was defined bigger in 1 position than actual size of dataReader.
solved it by definening the byte array samller in 1 position:

Dim myBytes(iFileLen - 1) As Byte
'iFileLen = the length of filestram that is stored in the db, in an image type field
l = objDR.GetBytes(colIndex, startIndex, myBytes, 0, iFileLen)
'objDR = the datareader that contains the binary data of a docx file that is stored in the db
Dim a As New IO.FileStream(myPath & "test1.docx", IO.FileMode.Create)
a.Write(myBytes, 0, myBytes.Length)

a.Close()
 
J

Jimit Shah

Hi Sunny,

Did you get the solution to the problem. I am facing the same problem and would appreciate your reply on the same as it has become a show stopper for me now.

My Code is given below to retrieve the docx content from SQL 2005 whose data type is image.

If (Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" Or Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") then
Response.OutputStream.Write objRS("docx_data")
else
Response.BinaryWrite l_objRS("docx_data")
End If
Response.Flush()
Response.Close()

Let me know if you require any other details from my end.
 

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

Top