小弟现在用的是ASP.NET+VB.NET
我想在ASP.NET页面中以字符流的形式上传文件到数据库中!
以及我想从ASP.NET页面中以字符流的形式从数据库下载文件到本地硬盘中!
请问各位大侠怎样做!
代码如下:
Public Sub btnupload_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupload.Click
If (fileupload.PostedFile.ContentType <> "") Then
Dim strdocext As String
Dim strdoctype As String
strdocext = Right(fileupload.PostedFile.FileName, 4).ToLower
Select Case (strdocext)
Case ".doc"
strdoctype = "doc"
Case ".ppt"
strdoctype = "ppt"
Case ".htm"
strdoctype = "htm"
Case ".html"
strdoctype = "htm"
Case ".jpg"
strdoctype = "jpg"
Case ".gif"
strdoctype = "gif"
Case ".txt"
strdoctype = "txt"
End Select
Dim intd As Integer = fileupload.PostedFile.ContentLength
Dim filebuffer(intd) As Byte
Dim filestr As FileStream = File.Open(fileupload.Value.Trim(), FileMode.Open)
filestr.Read(filebuffer, 0, filestr.Length)
Dim connection As String = System.Configuration.ConfigurationSettings.AppSettings("ConntionString")
Dim conn As New SqlConnection(connection)
conn.Open()
'Dim sql As String = "uploadfile"
Dim cmd As New SqlCommand("uploadfile", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@doctitle", SqlDbType.Char, 10)
cmd.Parameters.Add("@doctype", SqlDbType.Char, 10)
cmd.Parameters.Add("@doc", SqlDbType.Image)
cmd.Parameters.Item(1).Value = txtfile.Text
cmd.Parameters.Item(2).Value = strdoctype
cmd.Parameters.Item(3).Value = filebuffer
cmd.ExecuteNonQuery()
conn.Close()
End If
End Sub