Monday, 10 December 2012

Upload image using ASP.NET and SQL Server

1) Create the below columns:

CREATE TABLE [dbo].[User](
[ID] [int] IDENTITY(1,1) NOT NULL,
[FileName] [nvarchar](max) NULL,
[ContentType] [nvarchar](255) NULL,
[FileContent] [varbinary](max) NULL)

2) Add upload control to your web page:
<asp:FileUpload ID="FileUpload_Image" runat="server"  />

3) Add the following handler class to your project:

FileName: Handler_Image.ashx

<%@ WebHandler Language="VB" Class="Handler_Image" %>

Imports System
Imports System.Web
Imports System.Data

Public Class Handler_Image : Implements IHttpHandler
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
       
        
        'Byte[] content = GetImageFromDB(id)
        Dim Content() As Byte
        Dim ContentType As String
        
        Dim oID As Object = context.Request.QueryString("ID")
        Dim oType As Object = context.Request.QueryString("Type")
                
        If Not IsNothing(oID) And Not IsNothing(oType) Then
            Dim ID As Integer = Convert.ToInt32(oID.ToString())
            Dim Type As Integer = Convert.ToInt32(oType.ToString())            
          
                    If Not IsNothing(ID) And IsNumeric(ID) Then
                    
                        Dim Dataset_File As DataSet = DBHelper.RunSQLQuery("SELECT FileName, ISNULL(ContentType,'') ContentType ,FileContent FROM [User] WHERE ID=" & ID)
                        
                        If Not IsDBNull(Dataset_File.Tables(0).Rows(0).Item("FileContent")) Then
                            ContentType = Dataset_File.Tables(0).Rows(0).Item("ContentType")
                            Content = Dataset_File.Tables(0).Rows(0).Item("FileContent")
                        End If
                                   
                    End If
     
        End If

        If Not IsNothing(Content) Then
            context.Response.BinaryWrite(Content)
        Else
            context.Response.ContentType = "text/plain"
            context.Response.Write("Hello World")
        End If
        
    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class



4) On Save Event:


If FileUpload_Image.HasFile Then
Dim contentType As String = FileUpload_Image.PostedFile.ContentType

Dim FileName As String = System.IO.Path.GetFileName(

FileUpload_Image.PostedFile.FileName)
Dim byteArray As Byte() = 
FileUpload_Image
.FileBytes

e.Command.Parameters("@FileName").Value = FileName
e.Command.Parameters("@ContentType").Value = contentType
e.Command.Parameters("@FileContent").Value = byteArray
End If


N.B. for pjpeg:
http://afsawaf.blogspot.com/2012/12/imagepjpeg-vs-imagejpg-mime-types.html


--22nd May, 2013

This is to answer the question of how to display this uploaded image:

<asp:Image ID="Image_Signature" runat="server" Height="100px" Width="200px" ImageUrl='<%# Eval("ID", "Handler_Image.ashx?ID={0}&Type=10&Unique=") + Now().ToString()%>' />

No comments:

Post a Comment