infinitizon
MIS
Hi all, I'm trying to upload files to my database but I've been having problems.
Here's what I have so far:
It allows most files except .docx, .xlsx, and office 2007 generally. What could be wrong?
____________________
Men put up a strong face just to cover their weaknesses...good!
But a smile makes them live longer....
Which would you choose?
Think about it.
Here's what I have so far:
Code:
Imports System
Imports System.IO
Imports System.Data
Imports System.Data.Odbc
Imports System.Data.SqlClient
Imports System.Configuration
Partial Class Files
Inherits System.Web.UI.Page
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
If Not (myFile.PostedFile Is Nothing) Then
Dim intFileNameLength As Integer
Dim strFileNamePath As String
Dim strFileNameOnly As String
'Logic to find the FileName (excluding the path)
strFileNamePath = myFile.PostedFile.FileName
intFileNameLength = InStr(1, StrReverse(strFileNamePath), "\")
strFileNameOnly = Mid(strFileNamePath, (Len(strFileNamePath) - intFileNameLength) + 2)
Dim dirPath As String = AppDomain.CurrentDomain.BaseDirectory + "Files"
myFile.PostedFile.SaveAs(dirPath & "\" & strFileNameOnly)
lblMsg.Text = "File Upload Success.<br />"
lblMsg.Text &= "Content type: " & myFile.PostedFile.ContentType & "<br />"
lblMsg.Text &= "File size: " & CStr(myFile.PostedFile.ContentLength) & " bytes<br />"
lblMsg.Text &= "File Name: " & strFileNameOnly
Dim fs As FileStream = New FileStream(strFileNamePath, FileMode.Open, FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(fs.Length)
'Dim stream As New FileStream(strFileNamePath, FileMode.Open, FileAccess.Read)
'Dim reader As New BinaryReader(stream)
'Dim file As Byte() = reader.ReadBytes(CInt(stream.Length))
'reader.Close()
'stream.Close()
br.Close()
fs.Close()
'insert the file into database
Dim strQuery As String = "insert into Intranet_File_Table(ContentType, fileName, fileDesc,uploadDate,revisedDate,"
strQuery &= "uploadByUser,modifiedByUser , fileBlobForm)"
strQuery &= "values (@ContentType,@fileName,@fileDesc,@uploadDate,@revisedDate,@uploadByUser,@modifiedByUser,@fileBlobForm)"
Dim cmd As SqlCommand = New SqlCommand(strQuery)
'cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = strFileNameOnly
'cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = myFile.PostedFile.ContentType
'cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes
cmd.Parameters.AddWithValue("@ContentType", myFile.PostedFile.ContentType)
cmd.Parameters.AddWithValue("@fileName", strFileNameOnly)
cmd.Parameters.AddWithValue("@fileDesc", txtDesc.Text)
cmd.Parameters.AddWithValue("@uploadDate", Date.Now)
cmd.Parameters.AddWithValue("@revisedDate", Date.Now)
cmd.Parameters.AddWithValue("@uploadByUser", My.User.Name)
cmd.Parameters.AddWithValue("@modifiedByUser", My.User.Name)
cmd.Parameters.AddWithValue("@fileBlobForm", bytes)
'cmd.Parameters.Add("@fileBlobForm", SqlDbType.Binary, file.Length).Value = file
InsertUpdateData(cmd)
End If
End Sub
Public Function InsertUpdateData(ByVal cmd As SqlCommand) As Boolean
Dim strConnString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString()
Dim con As New SqlConnection(strConnString)
cmd.CommandType = CommandType.Text
cmd.Connection = con
Try
con.Open()
cmd.ExecuteNonQuery()
Return True
Catch ex As Exception
Response.Write(ex.Message)
Return False
Finally
con.Close()
con.Dispose()
End Try
End Function
End Class
It allows most files except .docx, .xlsx, and office 2007 generally. What could be wrong?
____________________
Men put up a strong face just to cover their weaknesses...good!
But a smile makes them live longer....
Which would you choose?
Think about it.