Skip to content Skip to sidebar Skip to footer

How to Upload a Blob File to Database in Php File Upload

OverviewToday, we will come across how to upload files, whether it's a Word file, pdf, zip files etc., and save them in the Oracle database every bit well as call up those files and download those. This code is useful when y'all need to upload various documents in an arrangement, including process documents, news then on. Other users will have to download those files and meet the contents. To see the content of what yous have uploaded, you accept to save information technology in Binary format. Let'due south commencement:

uploaded

Stride i : Lets create a tabular array first Showtime, yous need to install the Oracle Database on your car and create databases in your system. The Oracle SQL Developer is a GUI to access those databases.

One time y'all are through with the installation and creation of the database, it is time to add together a connection.

Note:

Earlier installing Oracle, yous need to add together TNS names in tnsnames.ora in gild to communicate with your databases.

Become to your system bulldoze where you had installed the Oracle. This is my path:

path

Open tnsname.ora file and add together a TNS entry there.

code

At present, configure the connection in your Oracle SQL Developer.

SQL

Click on Connect. It will add together a connection successfully. We have continued to our database successfully .

database

Now, let's create a table.

table

Wait at the Data type in Oracle carefully. I made ID as chief key. Oracle doesn't identify the GUI. And then, you have to write a query for this .

The name I gave is Varchar2 with a length of 4000 Bytes (maximum).
Content type is again varchar2 which is again 4000 Bytes long (maximum).

Data is stored in BLOB. ( Remember in the previous article in SQL server we had been storing data in Binary Format). Here, in Oracle,  we will be saving Data in BLOB .

Once we accept created the tabular array, it'southward time to create id every bit autoincrement.

Simply blazon this command,

command

It will create a sequence, doc_id. Now, just run across by selecting the argument if your sequence has been created or not .

command

Step two : Open Visual Studio

Open Visual Studio File->New Website.

New Website

Select ASP.NET Empty Website and Give suitable name as DocumentSaveInBinary,

DocumentSaveInBinary

Now, allow's create FileUpload Control as,

code

  1. < asp:FileUpload ID = "FileUpload1" runat = "server" />
  2. < asp:Button ID = "btnUpload" runat = "server" Text = "Upload" OnClick = "Upload" CssClass = "btn-master" />

Now, create Gridview with Download Link button, then that nosotros can download respective documents or Files .

code

  1. < asp:GridView ID = "GridView1" runat = "server"
  2. AutoGenerateColumns = "false" CssClass = "table" >
  3. < Columns >
  4. < asp:BoundField DataField = "Name" HeaderText = "File Name" />
  5. < asp:TemplateField ItemStyle-HorizontalAlign = "Centre" >
  6. < ItemTemplate >
  7. < asp:LinkButton ID = "lnkDownload" runat = "server" Text = "Download" OnClick = "DownloadFile"
  8. CommandArgument = '<%# Eval("Id") %>' > </ asp:LinkButton >
  9. </ ItemTemplate >
  10. </ asp:TemplateField >
  11. </ Columns >
  12. </ asp:GridView >

Here you will see that inside Gridview used <asp:BoundField/> Showing HeaderText as FileName in gridview. In that <Itemtemplate></Itemtemplate> Within Itemtemplate you demand to bind Link button with ID="lnkDownload" OnClick="DownloadFile".

Then my final Document.aspx lawmaking is,

  1. < %@ Page Language = "C#" AutoEventWireup = "true" CodeFile = "DocumentUpload.aspx.cs" Inherits = "_Default"  % >
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML i.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. < html xmlns = "http://www.w3.org/1999/xhtml" >
  4. < head id = "Head1" runat = "server" >
  5. < championship > </ title >
  6. < link rel = "Stylesheet" href = "Styles/bootstrap.min.css" style = "" />
  7. < link rel = "Stylesheet" href = "Styles/bootstrap.css" />
  8. </ caput >
  9. < body >
  10. < course id = "form1" runat = "server" >
  11. < div class = "container" >
  12. < asp:FileUpload ID = "FileUpload1" runat = "server" />
  13. < asp:Button ID = "btnUpload" runat = "server" Text = "Upload" OnClick = "Upload" CssClass = "btn-main" />
  14. < hour />
  15. < asp:GridView ID = "GridView1" runat = "server"
  16. AutoGenerateColumns = "fake" CssClass = "table" >
  17. < Columns >
  18. < asp:BoundField DataField = "Name" HeaderText = "File Name" />
  19. < asp:TemplateField ItemStyle-HorizontalAlign = "Center" >
  20. < ItemTemplate >
  21. < asp:LinkButton ID = "lnkDownload" runat = "server" Text = "Download" OnClick = "DownloadFile"
  22. CommandArgument = '<%# Eval("Id") %>' > </ asp:LinkButton >
  23. </ ItemTemplate >
  24. </ asp:TemplateField >
  25. </ Columns >
  26. </ asp:GridView >
  27. </ div >
  28. </ form >
  29. </ body >
  30. </ html >

Step 3 : Now let'south meet CS Lawmaking Part

Our File Upload Code is :

code

As you lot see in the above code,  we are saving our tabular array FileName,Contentype; hither the content types are word, pdf, image and so on and then we are saving the posted file in binary format past using Stream. File which you had uploaded in Fileupload Control and is converted in the binary file through BinaryReader .

Hither, I have added a connexion string in the code .

Now, while adding, you demand to insert the id. Encounter, what I had inserted:  sequencename.next val

When we were inserting in SQL, we were using "@" . Hither, in Oracle, we are using ":":

  1. string  constr = "User ID=axis_nsdl;Password=a$xis_4321;Information Source=TSTDPSEC" ;
  2. using  (OracleConnection con = new  OracleConnection(constr))
  3.              {
  4. string  query = "insert into tblFiles values (doc_id.nextval,:Name, :ContentType, :Data)" ;
  5. using  (OracleCommand cmd = new  OracleCommand(query))
  6.                  {
  7.                      cmd.Connection = con;
  8.                      cmd.Parameters.AddWithValue(":Name" , filename);
  9.                      cmd.Parameters.AddWithValue(":ContentType" , contentType);
  10.                      cmd.Parameters.AddWithValue(":Data" , bytes);
  11.                      con.Open();
  12.                      cmd.ExecuteNonQuery();
  13.                      con.Close();
  14.                  }
  15.              }
  16.          }
  17.      }
  • Similarly, we volition write the code for download, as we had created Onclick in gridview every bit Download File

    code

    Now, we Volition Demark the gridView :

    code

    So my Terminal CS Code is

    1. using  System;
    2. using  Organisation.Collections.Generic;
    3. using  System.Linq;
    4. using  System.Web;
    5. using  System.Information;
    6. using  Organisation.Data.SqlClient;
    7. using  System.IO;
    8. using  Organization.Web.UI.WebControls;
    9. using  Arrangement.Spider web;
    10. using  System.Configuration;
    11. using  System.Data.OracleClient;
    12. public  partial class  _Default : System.Spider web.UI.Page
    13. {
    14. private  OracleConnection con = new  OracleConnection( "User ID=axis_nsdl;Countersign=a$xis_4321;Data Source=TSTDPSEC" );
    15. protected void  Page_Load( object  sender, EventArgs east)
    16.     {
    17.         BindGrid();
    18.     }
    19. private void  BindGrid()
    20.     {
    21. string  constr = "User ID=axis_nsdl;Password=a$xis_4321;Data Source=TSTDPSEC" ;
    22. using  (OracleConnection con = new  OracleConnection(constr))
    23.         {
    24. using  (OracleCommand cmd = new  OracleCommand())
    25.             {
    26.                 cmd.CommandText ="select Id, Name from tblFiles" ;
    27.                 cmd.Connection = con;
    28.                 con.Open up();
    29.                 GridView1.DataSource = cmd.ExecuteReader();
    30.                 GridView1.DataBind();
    31.                 con.Close();
    32.             }
    33.         }
    34.     }
    35. protected void  Upload( object  sender, EventArgs e)
    36.     {
    37. string  filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
    38. string  contentType = FileUpload1.PostedFile.ContentType;
    39. using  (Stream fs = FileUpload1.PostedFile.InputStream)
    40.         {
    41. using  (BinaryReader br = new  BinaryReader(fs))
    42.             {
    43. byte [] bytes = br.ReadBytes((Int32)fs.Length);
    44. string  constr = "User ID=axis_nsdl;Countersign=a$xis_4321;Data Source=TSTDPSEC" ;
    45. using  (OracleConnection con = new  OracleConnection(constr))
    46.                 {
    47. cord  query = "insert into tblFiles values (doc_id.nextval,:Name, :ContentType, :Data)" ;
    48. using  (OracleCommand cmd = new  OracleCommand(query))
    49.                     {
    50.                         cmd.Connectedness = con;
    51.                         cmd.Parameters.AddWithValue(":Name" , filename);
    52.                         cmd.Parameters.AddWithValue(":ContentType" , contentType);
    53.                         cmd.Parameters.AddWithValue(":Data" , bytes);
    54.                         con.Open();
    55.                         cmd.ExecuteNonQuery();
    56.                         con.Close();
    57.                     }
    58.                 }
    59.             }
    60.         }
    61.         Response.Redirect(Request.Url.AbsoluteUri);
    62.     }
    63. protected void  DownloadFile( object  sender, EventArgs e)
    64.     {
    65. int  id = int .Parse((sender as  LinkButton).CommandArgument);
    66. byte [] bytes;
    67. string  fileName, contentType;
    68. string  constr = "User ID=axis_nsdl;Password=a$xis_4321;Information Source=TSTDPSEC" ;
    69. using  (OracleConnection con = new  OracleConnection(constr))
    70.         {
    71. using  (OracleCommand cmd = new  OracleCommand())
    72.             {
    73.                 cmd.CommandText ="select Name, Data, ContentType from tblFiles where Id=:Id" ;
    74.                 cmd.Parameters.AddWithValue(":Id" , id);
    75.                 cmd.Connection = con;
    76.                 con.Open();
    77. using  (OracleDataReader sdr = cmd.ExecuteReader())
    78.                 {
    79.                     sdr.Read();
    80.                     bytes = (byte [])sdr[ "Data" ];
    81.                     contentType = sdr["ContentType" ].ToString();
    82.                     fileName = sdr["Proper name" ].ToString();
    83.                 }
    84.                 con.Close();
    85.             }
    86.         }
    87.         Response.Clear();
    88.         Response.Buffer =true ;
    89.         Response.Charset ="" ;
    90.         Response.Enshroud.SetCacheability(HttpCacheability.NoCache);
    91.         Response.ContentType = contentType;
    92.         Response.AppendHeader("Content-Disposition" , "attachment; filename="  + fileName);
    93.         Response.BinaryWrite(bytes);
    94.         Response.Flush();
    95.         Response.End();
    96.     }
    97. protected void  BtnSearch_Click( object  sender, System.EventArgs east)
    98.     {
    99.         con.Open();
    100.         OracleCommand cmd =new  OracleCommand( "Select * from Proper name where tblFiles like '"  + txtSearch.Text + "%'" , con);
    101.         OracleDataAdapter da =new  OracleDataAdapter(cmd);
    102.         DataSet ds =new  DataSet();
    103.         da.Make full(ds);
    104.         con.Close();
    105.         GridView1.DataSource = ds;
    106.         GridView1.DataBind();
    107.     }
    108. protected void  btnBack_Click( object  sender, System.EventArgs eastward)
    109.     {
    110.         Response.Redirect("alphabetize.html" );
    111.     }
    112. }
  • Just Run the Application Debug on Activity Events to see the FileUpload and its content.

    Application

    application

    Nosotros accept successfully uploaded the file. At present, let's download and encounter the information .

    data

    data

    We have successfully downloaded . Now, let'southward meet the Oracle tabular array part

    table part

    As you tin can see, the data is stored in BLOB format successfully.

johnswoperand1967.blogspot.com

Source: https://www.c-sharpcorner.com/article/upload-files-and-save-in-oracle-database-in-blob-format-in-asp-net/

Postar um comentário for "How to Upload a Blob File to Database in Php File Upload"