Wednesday 13 June 2012

ASP.NET & C# - FILE UPLOAD TO SQL SERVER DATABASE

Asp.net accepts two types of file upload operations :

  1. Uploading a file to the file system 
  2. Uploading file to the database on the server 
  • Uploading a file to the file system:
To save the file to the file system ,the windows account associated with ASP.NET page must have sufficient permission to save the file .For windows 2003 and windows 2008 servers.an the page performs in the security context of the NETWORK SERVICE account.

Disadvantage:

To enable ASP.NET framework to save an uploaded file , you need to:
 right click the folder within windows explorer -> select the security tab->provide either the NETWORK SERVICE or ASP.NET account->write permission for the folder.

  • Uploading file to the database on the server



To upload a file to the SQL server database table in asp.net we use the FileUpload control .Retrieving a file from a database table on the server places more stress on the server.

Advantages:

  • Avoid file system permission issues 
  • Saving a file to database table enable you to more easily back up your information

FileUpload Control:


Supports the following properties:

  • Enabled-Enables you to disable the FileUpload Control.
  • FileBytes-Enables you to get the uploaded file contents as a byte array.
  • FileContent-Enables you to get the uploaded file contents as a stream.
  • FileName- Enables you to get the name of the uploaded file
  • HasFile-Returns True when the file has been uploaded.
  • PostedFile- Enables you to get the uploaded file wrapped in the HttpPostedFile
  • Focus- Enables you to shift the form focus to the  FileUploaded Control
  • SaveAs- Enables you to save the uploaded file to the file system.

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
                
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <br />
        <br />
        File Name:
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <br />
    Author: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br />
    <asp:Button ID="Upload" Text="Upload Now" runat="server" onclick="Upload_Click" />
    </div>
    <p>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </p>
    </form>
</body>
</html>

Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDBFileName=|DataDirectory|\\Database.mdf;Integrated Security=true;user instance=true;");
   
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Upload_Click(object sender, EventArgs e)
    {
        if(FileUpload1.HasFile)
        {
            byte[] img=new byte[FileUpload1.PostedFile.ContentLength];
            HttpPostedFile myfile=FileUpload1.PostedFile;
            myfile.InputStream.Read(img,0,FileUpload1.PostedFile.ContentLength);
           
            SqlCommand cmd=new SqlCommand("Insert into Table1(author,filecontent) values(@filename,@file)",conn);
            SqlParameter flname=new SqlParameter("@filename",System.Data.SqlDbType.VarChar,25);
            flname.Value=TextBox1.Text;
            cmd.Parameters.Add(flname);
            SqlParameter fl=new SqlParameter("@file",System.Data.SqlDbType.Binary);
            fl.Value=img;
            cmd.Parameters.Add(fl);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            Label1.Text="File Uploaded";

        }
        else{
            Label1.Text="Can't upload file";
        }


    }
}

No comments:

Post a Comment