H
Haim
I have an application that saves word files to a SQL2005 database and
retrieves them on demand. It works fine for files saved in the doc format,
but if the file is saved from the docx format, I cannot open it in my code.
When I open the file retrieved from the database manually in word 2007, I get
an error message "The file is corrupted and cannot be open". then I get a
second dialog box "some of the content cannot be read, if you trust the souce
word will attempt to open it" and then it does.
I have similar code which automates word and has it save the file to the
database. If I save the file in the old format I have no problems with it.
If I save the file in the docx format, than I cannot reoppen it.
Here is the code for retrieving the document:
Dim myrow As bardpagesDS.SelTempPropFilesRow =
myfileds.SelTempPropFiles.Rows(0)
Dim mybuffer() As Byte
Dim myext As String
Select Case filetype
Case "abstract"
mybuffer = myrow.AbstractFile
myext = myrow.ABSFiletype
Case "body"
mybuffer = myrow.ProposalFile
myext = myrow.PropFileType
End Select
If My.Computer.FileSystem.FileExists(filetype + "." + myext) Then
My.Computer.FileSystem.DeleteFile(filetype + "." + myext)
End If
Dim myfile As New IO.FileStream(filetype + "." + myext,
IO.FileMode.Create, IO.FileAccess.ReadWrite)
myfile.Write(mybuffer, 0, mybuffer.Length)
myfile.Close()
Return filetype + "." + myext
Code Which Uploads file
Public Function uploadfile(ByVal mytcn As Integer, ByVal mypw As String,
ByVal myfile As HttpPostedFile, ByVal filetype As String) As String
Try
Dim myarray(myfile.ContentLength) As Byte
Dim mybr As New System.IO.BinaryReader(myfile.InputStream)
mybr.Read(myarray, 0, myfile.ContentLength)
mybr.Close()
Dim c As String = System.IO.Path.GetFileName(myfile.FileName)
c = Right(c, 3)
Dim myscmd As Data.SqlClient.SqlCommand
myscmd = Me.SCMD_AddAbstractFile
With myscmd
.Parameters(1).Value = mytcn
.Parameters(2).Value = mypw
.Parameters(3).Value = c
.Parameters(4).Value = myarray
.Connection.Open()
.ExecuteNonQuery()
.Connection.Close()
End With
Return "OK"
Catch ex As Exception
Return ex.Message
End Try
End Function
--
Haim Katz
BARD
Was this post helpful to you?
Why should I rate a post?
retrieves them on demand. It works fine for files saved in the doc format,
but if the file is saved from the docx format, I cannot open it in my code.
When I open the file retrieved from the database manually in word 2007, I get
an error message "The file is corrupted and cannot be open". then I get a
second dialog box "some of the content cannot be read, if you trust the souce
word will attempt to open it" and then it does.
I have similar code which automates word and has it save the file to the
database. If I save the file in the old format I have no problems with it.
If I save the file in the docx format, than I cannot reoppen it.
Here is the code for retrieving the document:
Dim myrow As bardpagesDS.SelTempPropFilesRow =
myfileds.SelTempPropFiles.Rows(0)
Dim mybuffer() As Byte
Dim myext As String
Select Case filetype
Case "abstract"
mybuffer = myrow.AbstractFile
myext = myrow.ABSFiletype
Case "body"
mybuffer = myrow.ProposalFile
myext = myrow.PropFileType
End Select
If My.Computer.FileSystem.FileExists(filetype + "." + myext) Then
My.Computer.FileSystem.DeleteFile(filetype + "." + myext)
End If
Dim myfile As New IO.FileStream(filetype + "." + myext,
IO.FileMode.Create, IO.FileAccess.ReadWrite)
myfile.Write(mybuffer, 0, mybuffer.Length)
myfile.Close()
Return filetype + "." + myext
Code Which Uploads file
Public Function uploadfile(ByVal mytcn As Integer, ByVal mypw As String,
ByVal myfile As HttpPostedFile, ByVal filetype As String) As String
Try
Dim myarray(myfile.ContentLength) As Byte
Dim mybr As New System.IO.BinaryReader(myfile.InputStream)
mybr.Read(myarray, 0, myfile.ContentLength)
mybr.Close()
Dim c As String = System.IO.Path.GetFileName(myfile.FileName)
c = Right(c, 3)
Dim myscmd As Data.SqlClient.SqlCommand
myscmd = Me.SCMD_AddAbstractFile
With myscmd
.Parameters(1).Value = mytcn
.Parameters(2).Value = mypw
.Parameters(3).Value = c
.Parameters(4).Value = myarray
.Connection.Open()
.ExecuteNonQuery()
.Connection.Close()
End With
Return "OK"
Catch ex As Exception
Return ex.Message
End Try
End Function
--
Haim Katz
BARD
Was this post helpful to you?
Why should I rate a post?