Response a binary from Sql server

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi to all,

I have stored in a database some binary files (pdfs, and gif images), i
would like to know how could i show it, in internet explorer when a person
enters for instance in a textbox the id of the file that want to view....

This is my Sql table...

ID int
File binary

Any information will be grateful.
 
hi.

i've stuck up an example of how to return image/jpeg content from a database
(from an aspx page)

the example query simply selects an image, content-type, size, etc from a
table contaniing images.
If it was holding say word docs, the content type would be something like
"application/msword" - you need to check this (from memory) instead of
"image/jpeg"
and instead of calling the Image.Save() method, you'd simply write the
memory stream to the Response stream. (the reason there's an image object
there is because I've chopped out a section that does image manipulation
before writing to the response)

In a nutshell, select the blob, get memory stream, write the response
stream. make sure the Response.ContentType is right for the data your
returning.

HTH
Sam

private void Page_Load(object sender, System.EventArgs e)
{
string _iid = Request.QueryString["imgid"]+"";

int _imgid = int.Parse(_iid.Length==0?"0":_iid);

System.Drawing.Image _img;

if (_imgid !=0)
{
System.Data.SqlClient.SqlConnection Con = new
System.Data.SqlClient.SqlConnection();

Con.ConnectionString = DAL.Helper.DatabaseConnectString;

System.String SqlCmd = "SELECT * FROM schema.imagetable WHERE img_seq =
@imgid";
System.Data.SqlClient.SqlCommand SqlCmdObj = new
System.Data.SqlClient.SqlCommand(SqlCmd, Con);
SqlCmdObj.Parameters.Add("@imgid", System.Data.SqlDbType.Int).Value =
_imgid;

Con.Open();

System.Data.SqlClient.SqlDataReader SqlReader =
SqlCmdObj.ExecuteReader();

SqlReader.Read();

Response.ContentType = (string)SqlReader["img_contenttype"];

System.IO.MemoryStream mem = new
System.IO.MemoryStream((byte[])SqlReader["img_src"],0,(int)SqlReader["img_sizebytes"]);

_img = System.Drawing.Image.FromStream(mem);

_img.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);

Con.Close();

}


Response.End();
}
 
Back
Top