Discussion:
DOCX FILE CORRUPTED WHEN RETRIEVED FROM SQL2005
(too old to reply)
2008-08-06 10:46:00 UTC
Permalink
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.
--
SUNNY
CLY
2008-09-16 15:09:01 UTC
Permalink
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!
Post by
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.
--
SUNNY
Judith Pl
2010-11-10 11:49:27 UTC
Permalink
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()
Post by
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.
--
SUNNY
Post by 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!
Submitted via EggHeadCafe
Silverlight 3D Animated Topic Selector With Titled Menu Items
http://www.eggheadcafe.com/tutorials/aspnet/1add6e1c-6c70-4c2f-8b1a-5187961e5414/silverlight-3d-animated-topic-selector-with-titled-menu-items.aspx
Jimit Shah
2010-12-28 10:47:45 UTC
Permalink
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.
Post by
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.
--
SUNNY
Post by 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!
Post by Judith Pl
Hi,
I had the same problem - byte array size was defined bigger in 1 position than actual size of dataReader.
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()
Submitted via EggHeadCafe
Microsoft SQL Server DBA For Beginners
http://www.eggheadcafe.com/training-topic-area/SQL-Server-DBA/6/SQL-Server-DBA.aspx
Loading...