ASP文件上传未向MySql数据库提交数据

时间:2016-12-28 19:31:37

标签: mysql asp.net file-upload

我有一个 ASP .NET Web Form 应用程序,用于将文件上传到 MySql 5.5 表。

当我单击提交按钮时,将执行查询,但在表中只有Identity列获取值。

文件属性的列,即FileName(varchar(50)),ContentType(varchar(50))  和内容(中等blob)是空白的。所有其他控件(例如文本框)从应用程序执行插入操作,但Asp:FileUpload控件除外。

我的代码如下:

Protected Sub UploadFile()
        Dim filename As String = Path.GetFileName(fuCollateral.PostedFile.FileName)
        Dim contentType As String = fuCollateral.PostedFile.ContentType
        Dim iFileLen As Integer
        Dim byteFileData As Byte()
        Dim uploadedFile As HttpPostedFile = fuCollateral.PostedFile
        If fuCollateral.HasFile Then
            uploadedFile = fuCollateral.PostedFile
            iFileLen = uploadedFile.ContentLength
            ReDim byteFileData(iFileLen)
            uploadedFile.InputStream.Read(byteFileData, 0, iFileLen)

            Using fs As Stream = fuCollateral.PostedFile.InputStream
                Using br As New BinaryReader(fs)
                    Dim bytes As Byte() = br.ReadBytes(DirectCast(fs.Length, Long))
                    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
                    Using con As New MySqlConnection(constr)
                        Dim query As String = "INSERT INTO imagefiles(FileName, ContentType, Content) VALUES (@FileName, @ContentType, @Content)"
                        Using cmd As New MySqlCommand(query)
                            cmd.Connection = con
                            cmd.Parameters.Add(New MySqlParameter("@FileName", filename))
                            cmd.Parameters.Add(New MySqlParameter("@ContentType", contentType))
                            cmd.Parameters.Add(New MySqlParameter("@Content", bytes))
                            con.Open()
                            cmd.ExecuteNonQuery()
                            con.Close()
                        End Using
                    End Using
                End Using
            End Using
        End If
    End Sub

按钮点击事件的代码:

Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

  UploadFile()

End Sub

点击提交后的表格截图: enter image description here

0 个答案:

没有答案