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:
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:
Open tnsname.ora file and add together a TNS entry there.
At present, configure the connection in your Oracle SQL Developer.
Click on Connect. It will add together a connection successfully. We have continued to our database successfully .
Now, let's create a 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,
It will create a sequence, doc_id. Now, just run across by selecting the argument if your sequence has been created or not .
Step two : Open Visual Studio
Open Visual Studio File->New Website.
Select ASP.NET Empty Website and Give suitable name as DocumentSaveInBinary,
Now, allow's create FileUpload Control as,
- < asp:FileUpload ID = "FileUpload1" runat = "server" />
- < 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 .
- < asp:GridView ID = "GridView1" runat = "server"
- AutoGenerateColumns = "false" CssClass = "table" >
- < Columns >
- < asp:BoundField DataField = "Name" HeaderText = "File Name" />
- < asp:TemplateField ItemStyle-HorizontalAlign = "Centre" >
- < ItemTemplate >
- < asp:LinkButton ID = "lnkDownload" runat = "server" Text = "Download" OnClick = "DownloadFile"
- CommandArgument = '<%# Eval("Id") %>' > </ asp:LinkButton >
- </ ItemTemplate >
- </ asp:TemplateField >
- </ Columns >
- </ 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,
- < %@ Page Language = "C#" AutoEventWireup = "true" CodeFile = "DocumentUpload.aspx.cs" Inherits = "_Default" % >
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML i.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- < html xmlns = "http://www.w3.org/1999/xhtml" >
- < head id = "Head1" runat = "server" >
- < championship > </ title >
- < link rel = "Stylesheet" href = "Styles/bootstrap.min.css" style = "" />
- < link rel = "Stylesheet" href = "Styles/bootstrap.css" />
- </ caput >
- < body >
- < course id = "form1" runat = "server" >
- < div class = "container" >
- < asp:FileUpload ID = "FileUpload1" runat = "server" />
- < asp:Button ID = "btnUpload" runat = "server" Text = "Upload" OnClick = "Upload" CssClass = "btn-main" />
- < hour />
- < asp:GridView ID = "GridView1" runat = "server"
- AutoGenerateColumns = "fake" CssClass = "table" >
- < Columns >
- < asp:BoundField DataField = "Name" HeaderText = "File Name" />
- < asp:TemplateField ItemStyle-HorizontalAlign = "Center" >
- < ItemTemplate >
- < asp:LinkButton ID = "lnkDownload" runat = "server" Text = "Download" OnClick = "DownloadFile"
- CommandArgument = '<%# Eval("Id") %>' > </ asp:LinkButton >
- </ ItemTemplate >
- </ asp:TemplateField >
- </ Columns >
- </ asp:GridView >
- </ div >
- </ form >
- </ body >
- </ html >
Step 3 : Now let'south meet CS Lawmaking Part
Our File Upload Code is :
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 ":":
- string constr = "User ID=axis_nsdl;Password=a$xis_4321;Information Source=TSTDPSEC" ;
- using (OracleConnection con = new OracleConnection(constr))
- {
- string query = "insert into tblFiles values (doc_id.nextval,:Name, :ContentType, :Data)" ;
- using (OracleCommand cmd = new OracleCommand(query))
- {
- cmd.Connection = con;
- cmd.Parameters.AddWithValue(":Name" , filename);
- cmd.Parameters.AddWithValue(":ContentType" , contentType);
- cmd.Parameters.AddWithValue(":Data" , bytes);
- con.Open();
- cmd.ExecuteNonQuery();
- con.Close();
- }
- }
- }
- }
- Similarly, we volition write the code for download, as we had created Onclick in gridview every bit Download File
Now, we Volition Demark the gridView :
So my Terminal CS Code is
- using System;
- using Organisation.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Information;
- using Organisation.Data.SqlClient;
- using System.IO;
- using Organization.Web.UI.WebControls;
- using Arrangement.Spider web;
- using System.Configuration;
- using System.Data.OracleClient;
- public partial class _Default : System.Spider web.UI.Page
- {
- private OracleConnection con = new OracleConnection( "User ID=axis_nsdl;Countersign=a$xis_4321;Data Source=TSTDPSEC" );
- protected void Page_Load( object sender, EventArgs east)
- {
- BindGrid();
- }
- private void BindGrid()
- {
- string constr = "User ID=axis_nsdl;Password=a$xis_4321;Data Source=TSTDPSEC" ;
- using (OracleConnection con = new OracleConnection(constr))
- {
- using (OracleCommand cmd = new OracleCommand())
- {
- cmd.CommandText ="select Id, Name from tblFiles" ;
- cmd.Connection = con;
- con.Open up();
- GridView1.DataSource = cmd.ExecuteReader();
- GridView1.DataBind();
- con.Close();
- }
- }
- }
- protected void Upload( object sender, EventArgs e)
- {
- string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
- string contentType = FileUpload1.PostedFile.ContentType;
- using (Stream fs = FileUpload1.PostedFile.InputStream)
- {
- using (BinaryReader br = new BinaryReader(fs))
- {
- byte [] bytes = br.ReadBytes((Int32)fs.Length);
- string constr = "User ID=axis_nsdl;Countersign=a$xis_4321;Data Source=TSTDPSEC" ;
- using (OracleConnection con = new OracleConnection(constr))
- {
- cord query = "insert into tblFiles values (doc_id.nextval,:Name, :ContentType, :Data)" ;
- using (OracleCommand cmd = new OracleCommand(query))
- {
- cmd.Connectedness = con;
- cmd.Parameters.AddWithValue(":Name" , filename);
- cmd.Parameters.AddWithValue(":ContentType" , contentType);
- cmd.Parameters.AddWithValue(":Data" , bytes);
- con.Open();
- cmd.ExecuteNonQuery();
- con.Close();
- }
- }
- }
- }
- Response.Redirect(Request.Url.AbsoluteUri);
- }
- protected void DownloadFile( object sender, EventArgs e)
- {
- int id = int .Parse((sender as LinkButton).CommandArgument);
- byte [] bytes;
- string fileName, contentType;
- string constr = "User ID=axis_nsdl;Password=a$xis_4321;Information Source=TSTDPSEC" ;
- using (OracleConnection con = new OracleConnection(constr))
- {
- using (OracleCommand cmd = new OracleCommand())
- {
- cmd.CommandText ="select Name, Data, ContentType from tblFiles where Id=:Id" ;
- cmd.Parameters.AddWithValue(":Id" , id);
- cmd.Connection = con;
- con.Open();
- using (OracleDataReader sdr = cmd.ExecuteReader())
- {
- sdr.Read();
- bytes = (byte [])sdr[ "Data" ];
- contentType = sdr["ContentType" ].ToString();
- fileName = sdr["Proper name" ].ToString();
- }
- con.Close();
- }
- }
- Response.Clear();
- Response.Buffer =true ;
- Response.Charset ="" ;
- Response.Enshroud.SetCacheability(HttpCacheability.NoCache);
- Response.ContentType = contentType;
- Response.AppendHeader("Content-Disposition" , "attachment; filename=" + fileName);
- Response.BinaryWrite(bytes);
- Response.Flush();
- Response.End();
- }
- protected void BtnSearch_Click( object sender, System.EventArgs east)
- {
- con.Open();
- OracleCommand cmd =new OracleCommand( "Select * from Proper name where tblFiles like '" + txtSearch.Text + "%'" , con);
- OracleDataAdapter da =new OracleDataAdapter(cmd);
- DataSet ds =new DataSet();
- da.Make full(ds);
- con.Close();
- GridView1.DataSource = ds;
- GridView1.DataBind();
- }
- protected void btnBack_Click( object sender, System.EventArgs eastward)
- {
- Response.Redirect("alphabetize.html" );
- }
- }
- Just Run the Application Debug on Activity Events to see the FileUpload and its content.
Nosotros accept successfully uploaded the file. At present, let's download and encounter the information .
We have successfully downloaded . Now, let'southward meet the Oracle tabular array 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"