图片文件上传入SQL库及显示代码 转
图片文件上传入SQL库代码:
html页代码:
<INPUT id="file1" type="file" name="file1" runat="server">
后台代码:
Public Class BinaryFile
' 文件编号
Public FileID As Int64
' 文件标题
Public FileTitle As String
' 文件内容
Public FileContents As Byte()
' 文件类型
Public FileType As String
End Class
'------------------------------------------上传文件操作开始---------------------------------------------
Dim objFile As Common.Data.BinaryFile = New Common.Data.BinaryFile
Dim intLen As Integer
intLen = file1.PostedFile.ContentLength
If intLen > 200000 Then
Response.Write("<script language='javascript'>alert('上传的文件超出了限定的大小!');history.back();</script>")
End If
ReDim objFile.FileContents(intLen)
file1.PostedFile.InputStream.Read(objFile.FileContents, 0, intLen)
objFile.FileType = file1.PostedFile.ContentType
Dim FilePath As String = file1.Value
If intDocLen <> 0 Then
If Mid(file1.PostedFile.ContentType, 1, 5) <> "image" Then
Response.Write("<script language=javascript>alert('请上传正确的图片类型!');history.back();</script>")
Response.End()
End If
End If
Dim flag As Integer = objFile.Add(objFile.FileContents, objFile.FileType)
If flag = 1 Then
Response.Write("<script language=javascript>alert('提交成功!');</script>")
Else
Response.Write("<script>alert('提交失败!');history.back();</script>")
Response.End()
End If
'-----------------------------------------上传文件操作结束---------------------------------------------
图片文件显示代码:
html页代码:
<asp:Image id="Image2" runat="server"></asp:Image>
后台代码:
Dim objFile As Common.Data.BinaryFile = New Common.Data.BinaryFile
Dim CID As Integer = CInt(Trim(Request.QueryString("CID")))
Dim ds As DataSet = New DataSet
ds = objFile .Get(CID)
If IsDBNull(ds.Tables(0).Rows(0).Item("FileType")) = False And Left(CStr(ds.Tables(0).Rows(0).Item("FileType")), 6) = "image/" Then
Response.ContentType = Convert.ToString(ds.Tables(0).Rows(0).Item("FileType"))
Response.BinaryWrite(ds.Tables(0).Rows(0).Item("FileContents"))
Else
Me.Image.ImageUrl = "images/wu.gif"
End If