Sergey Poberezovskiy wrote:
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.
"MattB" wrote:
>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
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
------------------
Thanks!
Matt