Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Saving images in the database to file.
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Saving images in the database to file.

 Post Reply Post Reply
Author
Leeb65 View Drop Down
Groupie
Groupie


Joined: 05 December 2003
Location: Germany
Status: Offline
Points: 62
Post Options Post Options   Thanks (0) Thanks(0)   Quote Leeb65 Quote  Post ReplyReply Direct Link To This Post Topic: Saving images in the database to file.
    Posted: 09 January 2006 at 6:59pm
Hi,

I have many images in a database and I need to save them to an image file before I show them in a web page.
 
most of them are gif, but I do have a few jpegs. I need them to be saved in the same format.

Can anyone tell me how this is done?

Thanks,
Lee


Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 09 January 2006 at 7:38pm
Do you actually want to store a copy on the disk? Not sure what the point of that would be as you already got them. Nonetheless, if you, what I think you want to do just display them, create a HTTP Handler (ashx file) with a querystring or so of an imageID. Set the ContentType of the ashx to a content type field in the database. Then basically just call the byte content from the database (I use a SQLDataReader here)


If Not (Request.Params("ImageId") Is Nothing) Then

                documentId = Int32.Parse(Request.Params("ImageId"))
            End If
 
If documentId <> -1 Then
                'SQLDB is my Data Object
                Dim documents As New SQLDB()
 
                Dim dBContent As SqlDataReader = documents.GetDocumentContent(imageId)
                dBContent.Read()
 
                ' Serve up the file by name
               
Response.AppendHeader("content-disposition", "filename=" & CStr(dBContent("FileFriendlyName")))
 
 
                Response.ContentType = CType(dBContent("ContentType"), String)
 
                Response.OutputStream.Write(CType(dBContent("Content"), Byte()), 0, CInt(dBContent("ContentSize")))
 
                Response.End()
 
            End If
 
Hope this bit of code makes sense, I just grabbed it out of one of my projects.
Back to Top
Leeb65 View Drop Down
Groupie
Groupie


Joined: 05 December 2003
Location: Germany
Status: Offline
Points: 62
Post Options Post Options   Thanks (0) Thanks(0)   Quote Leeb65 Quote  Post ReplyReply Direct Link To This Post Posted: 10 January 2006 at 6:54am
Thanks Michael,
 
here is what I ended up with:
 

string sPath = "c:\\inetpub\\wwwroot\\iwkoeln\\import\\";

string sFileName="";

string sFilePath="";

divContent.Visible = false;

divBanner.Visible = false;

divImageP.Visible = true;

sqlDaImage.SelectCommand.Parameters["@ID"].Value = Convert.ToInt32(sId);

sqlDaImage.Fill(dsBericht);

if(dsBericht.Tables[0].Rows.Count>0)

{

if(!File.Exists(dsBericht.Tables[0].Rows[0]["strImagePicName"].ToString()))

{

baImage = (byte[])dsBericht.Tables[0].Rows[0]["imgImagePic"];

try

{

sFileName = dsBericht.Tables[0].Rows[0]["strImagePicName"].ToString();

if(sFileName.IndexOf("wwwroot")<=0)

{

sFilePath = sPath + sFileName;

}

else

{

int iWeg = sFileName.LastIndexOf("\\");

sFileName = sFileName.Substring(iWeg, sFileName.Length - iWeg);

sFilePath = sPath + sFileName;

}

FileStream fs = new FileStream(sFilePath, FileMode.Create);

BinaryWriter bw = new BinaryWriter(fs);

bw.Write(baImage);

bw.Close();

fs.Close();

}

catch(Exception ex)

{

string sError = ex.Message;

}

 

}

imgShow.Src = "import/" + sFileName;

}

I need the pictures on the disk for speed purposes. From the DB it took too long and used almost the full bandwidth. Yesterdeay was release day for the web site and it was a killer too, at one point I was waiting 10 Minutes for the homepage to show.


Edited by Leeb65 - 10 January 2006 at 6:59am
Lee


Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.