Hi,
I use ASyncFileUpload control in my web application to upload jpeg and gif files.
<asp:AsyncFileUpload runat="server" ID="FileUpload1" Width="400px" UploaderStyle="Modern"
UploadingBackColor="#CCFFFF" />
I save it in database as
If FileUpload1.PostedFile Is Nothing OrElse _
String.IsNullOrEmpty(FileUpload1.PostedFile.FileName) OrElse _
FileUpload1.PostedFile.InputStream Is Nothing Then
Label5.Style("font-weight") = "bold"
Label5.Style("color") = "red"
Label5.Text = "Please Upload Valid picture file"
Exit Sub
End If
'Make sure we are dealing with a JPG or GIF file
Dim strExtension As String = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower()
Dim sMIMEType As String = Nothing
Select Case strExtension
Case ".gif"
sMIMEType = "image/gif"
Case ".jpg", ".jpeg", ".jpe"
sMIMEType = "image/jpeg"
Case ".png"
sMIMEType = "image/png"
Case ".bmp"
sMIMEType = "image/bmp"
Case ".tif", ".tiff"
sMIMEType = "image/tif"
Case Else
'Invalid file type uploaded
Label5.Style("font-weight") = "bold"
Label5.Style("color") = "red"
Label5.Text = "Not a Valid file format"
Exit Sub
End Select
'Connect to the database and insert a new record into Products
Using oSQLConnection As New SqlConnection(ConfigurationManager.AppSettings("SQLAppDataConnStr"))
Const sSQL As String = "INSERT INTO [TESTIMAGE] ([Title], [Image], [MIMEType]) VALUES ( @Title, @ImageData, @MIMEType)"
Dim sSQLCommand As New SqlCommand(sSQL, oSQLConnection)
Dim sStr1 As String
sStr = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)
sStr1 = sStr.Substring(0, sStr.IndexOf("."))
sSQLCommand.Parameters.AddWithValue("@Title", sStr1)
'Load FileUpload's InputStream into Byte array
Dim imageBytes(FileUpload1.PostedFile.InputStream.Length) As Byte
FileUpload1.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length)
sSQLCommand.Parameters.AddWithValue("@ImageData", imageBytes)
sSQLCommand.Parameters.AddWithValue("@MIMEType", sMIMEType)
oSQLConnection.Open()
sSQLCommand.ExecuteScalar()
Dim query As String = "SELECT @@IDENTITY"
sSQLCommand.CommandText = query
Dim newid As Integer = sSQLCommand.ExecuteScalar()
hdImageID.Value = newid.ToString
I click another button but the image is not visible in the browser .
Dim nImageID As Integer = Convert.ToInt32(Request.QueryString("ImageID"))
'Connect to the database and bring back the image contents & MIME type for the specified picture
Using oSQLConnection As New SqlConnection(ConfigurationManager.AppSettings("SQLAppDataConnStr"))
Const sSQL As String = "SELECT [MIMEType], [IMAGE], [TITLE] FROM [TESTIMAGE] WHERE [ID] = @ImageID" ' Image is of type IMAGE type in sql server 2005 database
Dim oSQLCommand As New SqlCommand(sSQL, oSQLConnection)
oSQLCommand.Parameters.AddWithValue("@ImageID", nImageID)
oSQLConnection.Open()
Dim oSQLReader As SqlDataReader = oSQLCommand.ExecuteReader
If oSQLReader.Read Then
Response.BinaryWrite(oSQLReader("Image"))
End If
I found the control saves file correctly in the physical path, the saved record in the database has image datalength. but viewing operation does not work on the saved image
blob. Strangely the viewing works just fine when I use normal asp.net FileUpload control.
What is wrong with the saved image blob?
Thanks,
Vijay