Click here to Skip to main content
15,889,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys

I have an application who stores files in a sql server database.
The problem is that while I can upload .docx file in database i can't open that.
I can open .doc file normally.
Here is the code:


VB
Private Sub viewtb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles viewtb.Click
        If lv.Items.Count = 0 Then
            Exit Sub
        End If
        lv.Visible = False
        wb.Visible = True
        id_docx = lv.SelectedItems.Item(0).Text
        tampil(id_docx, "View")
        viewtb.Enabled = False
        SaveTb.Enabled = False
        PrintTB.Enabled = False
        closewb.Enabled = True
    End Sub

    Private Sub tampil(ByVal Idx As Integer, ByVal vt As String)
        Kill_proses()
        System.Threading.Thread.Sleep(500)
        Dim objNewFileStream As System.IO.FileStream = Nothing
        If My.Computer.FileSystem.DirectoryExists(temp) = True Then
            My.Computer.FileSystem.DeleteDirectory(temp, FileIO.DeleteDirectoryOption.DeleteAllContents)
        End If
        My.Computer.FileSystem.CreateDirectory(temp)
        Try
            Dim isigroup As DataTable
            objdata = New clsMSSQL
            isigroup = objdata.QueryDatabase("select * from docu where id=" & id_docx)
            If isigroup.Rows.Count = 1 Then
                Dim nama_file As String = isigroup.Rows(0).Item(4).ToString
                isigroup = objdata.QueryDatabase("Select * from docs where docu_id =" & id_docx)
                Dim bytes() As Byte
                Dim imgStream As MemoryStream
                id_img = isigroup.Rows(0).Item(0)
                bytes = CType(isigroup.Rows(0).Item(2), Byte())
                imgStream = New MemoryStream(bytes)
                imgStream.Position = 0
                objNewFileStream = New System.IO.FileStream(temp & "/" & nama_file, System.IO.FileMode.Create, System.IO.FileAccess.Write)
                Dim objBuffer(256) As Byte
                Dim intBytesRead As Integer = imgStream.Read(objBuffer, 0, 256)
                While intBytesRead > 0
                    objNewFileStream.Write(objBuffer, 0, intBytesRead)
                    intBytesRead = imgStream.Read(objBuffer, 0, 256)
                End While
                If Not imgStream Is Nothing Then
                    imgStream.Close()
                End If
                If Not objNewFileStream Is Nothing Then objNewFileStream.Close()
                Select Case vt
                    Case "View"
                        wb.Focus()
                        wb.Navigate(temp & "\" & nama_file)
                    Case "Print"
                        wb.Url = New Uri(temp & "\" & nama_file)
                        wb.Print()
                End Select
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
            Me.Close()
        End Try
    End Sub

 Private Function cek_ext(ByVal ext As String) As String
        Dim fileDetail As IO.FileInfo
        If ext = "" Then
        End If
        fileDetail = My.Computer.FileSystem.GetFileInfo(ext)
        cek_ext = (fileDetail.Extension)
        If cek_ext = ".txt" Or cek_ext = ".TXT" Then
            cek_ext = "Text File|*.txt"
        ElseIf cek_ext = ".pdf" Or cek_ext = ".PDF" Then
            cek_ext = "Adobe Acrobat|*.pdf"
        ElseIf cek_ext = ".bmp" Or cek_ext = ".BMP" Then
            cek_ext = "Bitmap |*.bmp"
        ElseIf cek_ext = ".jpg" Or cek_ext = ".JPG" Then
            cek_ext = "JPEG File|*.jpg"
        ElseIf cek_ext = ".doc" Or cek_ext = ".DOC" Then
            cek_ext = "Word Doc |*.doc"
        ElseIf cek_ext = ".docx" Or cek_ext = ".DOCX" Then
            cek_ext = "Word Doc |*.docx"
        ElseIf cek_ext = ".xls" Or cek_ext = ".XLS" Then
            cek_ext = "Excel Files |*.xls"
        End If
    End Function
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900