Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need to display binary data from SQL SERVER to the browser

Status
Not open for further replies.

sereleg

Programmer
Mar 13, 2005
26
0
0
US

I need help to display binary(could be pdf,doc,txt,etc) data stored in SQL server to the browser.

table=FileData

id int
FileType varchar
FileLength int
description varchar
FileData image


I described the sql server table so you can have an idea of the datatype I work with.

I will appreciate any help.

Thanks
 
I would retrieve the binary data from the database and save it on the disk. Next make it accessible to the browser by sending to the client the simple javascript:
Code:
<a href="\\mypath\\myfile.pdf" > Click here to see ...</a>
<a href="\\mypath\\myfile.doc" > Click here to see ...</a>
<a href="\\mypath\\myfile.txt" > Click here to see ...</a>
Or,in the .aspx page do the same using a LinkButton or LinkLabel if the client is a Windows application.
To retrive the binary from SQL :
[/code]
SqlConnection conn=new SqlConnection(sConnectionString);
SqlCommand cmd = new SqlCommand("select binary FROM FileData);
conn.Open();
SqlDataAdapter da=new SqlDataAdapter();
DataSet ds = new DataSet();
da.Fill(ds);
MemoryStream ms = new MemoryStream();
Bitmap bmp = null;
Byte [] bImg=null;
//int iOffset = 78; // offset fro DB images
for (iRowCount=0;i<ds.Tables[0].Rows.Count;iRowCount++)
{
bImg = ds.Tables[0].Rows[iRowCount].Item[0];
ms.Write(bImg,bImg.Length -/*iOffset*/);
bmp = new Bitmap(ms);
string sFileName= "File"+ds.Tables[0].Rows[iRowCount]["id"].ToString()+"."+ds.Tables[0].Rows[iRowCount]["FileType"];
bmp.Save(sFileName);
}
conn.Close();
//..
[/code]
There are also other ways.
obislavu
 
Obislavu
1.-Do you think it should be better to store that binary files directly to the hard disk INSTEAD TO THE database?

2.-There is a way to get the data displayed directly from DATABASE to the browser?
 
1. No, databases are recoverable from their logs, files aren't.
 
You can implement IHttpHandler in a class in order to direct data to the browser without going through a file, but it's a little more complicated. I would do some reading on it before trying to code up something.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I agree with Stevexff.
The method to download the binary file from the database and store on the disk could be useful in the case when other users request the same file. The first request will create the file and the subsequent requests will check if the file is there and in this case should be not download from database unless the database changed and you have a way to download only when the file is requested is not in sync with the database.
obislavu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top