Print Page | Close Window

Saving images in the database to file.

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=17808
Printed Date: 29 March 2026 at 3:12am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Saving images in the database to file.
Posted By: Leeb65
Subject: Saving images in the database to file.
Date 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

http://www.rheinsitemedia.de">



Replies:
Posted By: michael
Date 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.


-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: Leeb65
Date 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.


-------------
Lee

http://www.rheinsitemedia.de">



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net