Thursday, 11 July 2013

Save and Retrieve files from SQL SERVER 2008 with ASP.Net

Create a Table with columns look like ID,FileName,Extension,BinaryData(VarBinary(MAX))

CREATE PROCEDURE [dbo].[InsertDocument]
      (@FileName varchar(50),
      @Extension varchar(5),
      @FileContent varbinary(max))
AS
BEGIN
      DECLARE @ID INT
      SET @ID=(SELECT ISNULL(MAX(ID),0)+1 FROM File_T)
      INSERT INTO File_T (ID,FileName, Extension, BinaryData)
      Values (@ID,@FileName,@Extension,@FileContent);
END


in ASP.net coding,

<tr>
      <td>
               <asp:FileUpload
ID="fileUploadDocument" runat="server" />
       </td>
       <td>

               <asp:Button ID="btnUpLoad" OnClick="btnUpLoad_Click" runat="server" Text="Upload"     CssClass="ButtonClass" />
               <asp:Button ID="btnDownLoad" OnClick="btnDownLoad_Click" runat="server" Text="Download" CssClass="ButtonClass" />
         </td>

 </tr>

protected void btnUpLoad_Click(object sender, EventArgs e)
    {
        try
        {

            if (fileUploadDocument.HasFile)
            {
                // Get the File name and Extension
                strFileName = Path.GetFileName(fileUploadDocument.PostedFile.FileName);
                strFileExtension = Path.GetExtension(fileUploadDocument.PostedFile.FileName);
                //
                // Extract the content of the Document into a Byte array
                int intlength = fileUploadDocument.PostedFile.ContentLength;
                Byte[] byteData = new Byte[intlength];
                fileUploadDocument.PostedFile.InputStream.Read(byteData, 0, intlength);
                //
                // Save the file to the DB

               // Call the stored procedure and pass the values.
                int i = PURBLL.Save(byteData, strFileExtension, strFileName);
                //
                //lblMsg.Text = "Document Uploaded Succesfully";
            }
        }
        catch (Exception ex)
        {
            //lblMsg.Text = " Error uploading Document: " + ex.Message.ToString();
        }
    }


File Retrieve


Create procedure as your wish to read binary data.
CREATE PROCEDURE FileDownload
AS
 BEGIN
   SELECT ID,FileName,Extension,BinaryData  FROM File_T  WHERE ID=4
 END

 

protected void btnDownLoad_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = PURBLL.FileDownload();
            DataRow DR = dt.Rows[0];
            Byte[] byteDoc ;
            byteDoc=(byte[])DR["BinaryData"];
            // Response.ContentType = "application/vnd.ms-word";
            //Response.ContentType = "application/vnd.ms-excel";
            //Response.ContentType = "application/pdf";
           Response.AddHeader("content-disposition", "attachment;filename=" + DR["FileName"].ToString() );
           Response.Cache.SetCacheability(HttpCacheability.NoCache);
           Response.BinaryWrite(byteDoc);

           Response.End();
        }
        catch (Exception ex)
        {
        }
    }


The upload file size limit is set to 4MB by default in ASP.NET

If we want to upload more than 4MB file we need to include the following line in web.config file under 
 <system.web>
<httpRuntime maxRequestLength="2097152" executionTimeout="9999999" />
 
For reference
http://www.codeproject.com/Tips/576395/Uploading-large-files-using-ASP-NET-and-IIS6-0 
 
  

No comments:

Post a Comment