jpeg from database to file

M

MattB

Hi. I have an asp.net 1.1 application that I need to retrieve a jpeg
image from a database and write that to the local (not client's) file
system.

What I'm doing is a club login page, and each club can have a logo
stored in the sql database. In the past I have streamed an image
directly to the browser, but this time I want to write it as a jpg file
to the local (to the application) file system. The reason being that
this will be reloaded a number of times and I want to cache it so it
doesn't need to be retrieved from the database every time someone visits
the page.

Can anyone give me any hints? Currently, I'm converting the image data
to a bit array. How do I save that array to a file? Thanks!

Matt
 
G

Guest

Matt,

You can use System.IO namespace:

Dim outStream As Stream = New FileStream(fileName, FileMode.CreateNew)
With New MemoryStream(bytes)
.WriteTo(outStream)
End With

Alternatively, you could use .Net Cache to store images - it is specifically
designed for cases like yours.
 
R

Roland Dick

Hi Matt,
Hi. I have an asp.net 1.1 application that I need to retrieve a jpeg
image from a database and write that to the local (not client's) file
system.

When you have the byte array, you can simply open a filestream and pass
the byte array to the write method.

As an alternative, create a memorystream, write your byte array into
that, then create an image from the memorystream and call the save
method on the image object. This is handy if you need to save in a
different format or need to do some other operations on the image anyway.

Keep in mind that the account your webapp is running under needs write
permissions to the directory you want to save the image into.

Hope this helps,

Roland
 
M

MattB

Sergey said:
Matt,

You can use System.IO namespace:

Dim outStream As Stream = New FileStream(fileName, FileMode.CreateNew)
With New MemoryStream(bytes)
.WriteTo(outStream)
End With

Alternatively, you could use .Net Cache to store images - it is specifically
designed for cases like yours.

Great. Thanks for the tip (and thanks to Roland too). I tried this and
I'm getting a file created, but it's size is 0 bytes. Here's the whole
block of code. Can you see where I'm going wrong?

Dim strHex As String = dtAct.Rows(0)("webgrpimg")
'strip out characters that mess up the conversion
If ConfigurationSettings.AppSettings("UseWebService") = "1" Then
'for what ever reason, when going through the web service, the line
endings need to be stripped like this
strHex = strHex.Replace(vbCr, Nothing)
strHex = strHex.Replace(vbLf, Nothing)
Else
strHex = strHex.Replace(vbCrLf, Nothing)
End If
strHex = strHex.Replace(vbTab, Nothing)

'Once the bad characters are stripped, convert hex string to bit array
Dim MyData(Len(strHex) / 2) As Byte
Dim i As Int32, er As String
For i = 0 To (Len(strHex) / 2) - 1
Try
MyData(i) = CByte(CLng("&H" & Mid(strHex, (2 * i) + 1, 2)))
Catch ex As Exception
er = ex.Message
End Try
Next

If Not Directory.Exists(Server.MapPath("images/clubs")) Then
Directory.CreateDirectory(Server.MapPath("images/clubs"))
End If
If Not Directory.Exists(Server.MapPath("images/clubs/" &
dtAct.Rows(0)("acct_name"))) Then
Directory.CreateDirectory(Server.MapPath("images/clubs/" &
dtAct.Rows(0)("acct_name")))
End If


Dim outStream As Stream = New FileStream(strLogoPath, FileMode.CreateNew)
With New MemoryStream(MyData)
.WriteTo(outStream)
End With
 
M

MattB

Never mind! I got it sorted out by adding a outStream.Close at the end.
Thanks again!

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top