Uploading & Downloading Files using ASP.NET

This article will explain about the steps to upload the file as BLOB into the DB. Also explains about the steps to render the file with the specific content type, so that it gets opened in approrpiate software related to the file type.

Upload and Render Files using ASP.Net

There are two ways to store files in the webserver; one as a physical and another is in DB as BLOB. Security wise there are lot of advantages in storing the file in DB (People usually say that the DB size will grow extensively if we do so, my answer for them has two points. Point 1 - Maintain a File storage DB which can make things more organised, Point 2 - Even if we're going to store it in Webserver physically, the HD space will grow, How about that??

This article will explain about the steps to upload the file as BLOB into the DB. Also explains about the steps to render the file with the specific content type, so that it gets opened in approrpiate software related to the file type. Typically, the user will be shown Open/Save/Cancel box on the file download.

Going to the database side, there are three things to be created to test the above. The table, the input SP and the output SP.

Table  Creation -

CREATE TABLE [dbo].[tblFile](
 [ID] [int] IDENTITY(1,1) NOT NULL,
 [FILE_CONTENT] [varbinary](max) NOT NULL,
 [CONTENT_TYPE] [varchar](50)  NOT NULL,
 [FILE_NAME] [varchar](50) NOT NULL
) ON [PRIMARY]

Input SP -

CREATE PROCEDURE [dbo].[prcStoreFile]
(
 @FILE_CONTENT varbinary(max),
 @CONTENT_TYPE varchar(50),
 @FILE_NAME varchar(50)
)
AS
BEGIN
 Insert into tblFile values(@FILE_CONTENT, @CONTENT_TYPE, @FILE_NAME)
END

Output SP -

CREATE PROCEDURE [dbo].[prcGetFile]
AS
BEGIN
 SELECT Top 1 FILE_CONTENT,CONTENT_TYPE,FILE_NAME from tblFile order by ID Desc
END

Application : Coming to the application side, there is just a few lines of code which does the stuff. Use a file upload control, Read the Bytes from HTTPPostedFile and upload the bytes as BLOB to the DB.

Core Code to Upload -

 Dim len As Integer = File.ContentLength
        Dim FILE_NAME, CONTENT_TYPE As String
        FILE_NAME = File.FileName.Substring(File.FileName.LastIndexOfAny("\") + 1)
        CONTENT_TYPE = File.ContentType
        Dim FILE_CONTENT(len) As Byte
        File.InputStream.Read(FILE_CONTENT, 0, len)

        cmd.Parameters.AddWithValue("@FILE_CONTENT", FILE_CONTENT)
        cmd.Parameters.AddWithValue("@CONTENT_TYPE", CONTENT_TYPE)
        cmd.Parameters.AddWithValue("@FILE_NAME", FILE_NAME)
        cmd.ExecuteNonQuery()

Code to Download - There are three things to be done to render the download document. After reading the BLOB from the DB, just change the ContentType Property and Response Header. After this, call the BinaryWrite method to render the BLOB in the desired way.

 'Content Uploaded in the Upload Step is read from Hashtable and rendered as below

 Response.ContentType = h.Item("ContentType") 
 Response.AddHeader("content-disposition", "attachment; filename=" & h.Item("FileName"))
 Response.BinaryWrite(h.Item("BinaryContent"))

Download the Sample Code Here

http://www.eggheadcafe.com/fileupload/1142530339_FileLoader.zip

By Aravind Kumar   Popularity  (6912 Views)