Forum Moderators: open

Message Too Old, No Replies

From Base64 to File

Have Base64 Encoded file and need to save it

         

qlipoth

6:23 pm on Aug 20, 2004 (gmt 0)

10+ Year Member



Hi, sorry if this sounds like total n00b work, but I'm stuck.

I have a base64-encoded file (a .gif image) passed to me through XML, sitting in a node object. I now need it saved to a temp directory on the server, and everything I can find on how to do this is in VB6.0 or .net I am, unfortunatly, limited to ASP.

sqlgod

5:06 am on Aug 21, 2004 (gmt 0)

10+ Year Member



Hope this helps :)

Public Function ConvertAndSaveBase64Image(ByVal imgBase64 As String, ByVal saveToDirectory As String, ByVal FileName As String)
Dim binaryData() As Byte
Dim outFile As System.IO.FileStream

binaryData = System.Convert.FromBase64String(imgBase64)

Try
outFile = New System.IO.FileStream(saveToDirectory & "\" & FileName, System.IO.FileMode.Create)

outFile.Write(binaryData, 0, binaryData.Length - 1)

outFile.Close()
outFile = Nothing

Catch exp As Exception
Throw New Exception(exp.Message)
Finally
End Try

End Function

sqlgod

5:09 am on Aug 21, 2004 (gmt 0)

10+ Year Member



ack sorry i guess i should read before posting :)

qlipoth

4:39 pm on Aug 21, 2004 (gmt 0)

10+ Year Member



Yeah, thus my problem. I'll be so much happier when we move to .net, but that's still a way off.

Xoc

4:46 pm on Aug 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need a DLL that does the translation for you. It could be written in VB6 or .NET using COM interop. If you do it in .NET, then the code above will work. You then instantiate and call the DLL.

qlipoth

7:35 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



OK, I've got a dll, and it makes a file, but the file is unreadable. The File being passed to me is from a vendor who does these things by the thousands, so I'm almost positive that I'm RECIEVING the file correctly... so the error must be something in my code. Here's what I've got:

The ASP:


set currNode = NewDoc.selectSingleNode("/ShipmentAcceptResponse/ShipmentResults/PackageResults/LabelImage/GraphicImage")
Set oFS = Server.CreateObject("ConvandSave.Class1")
dim strdebug
strdebug=oFS.ConvertAndSaveBase64Image(currnode, strFilename)

And the Class itself (In VB6)


Public Function ConvertAndSaveBase64Image(ByVal oNode As IXMLDOMNode, ByVal strFileName As String) As String
Dim iFile As Integer
Dim arrBuffer() As Byte

On Error GoTo errHandle

arrBuffer = oNode.nodeTypedValue


iFile = FreeFile()
Open strFileName For Binary Access Write As iFile
Put iFile, , arrBuffer
Close iFile

ConvertAndSaveBase64Image = "Success"
Exit Function
errHandle:
ConvertAndSaveBase64Image = Error(Err)
End Function

The class is based on (Hell, it's ripped off from) code I found at [support.microsoft.com ] And it DOES produce a file, but not a readable one. Anyone spot my mistake?

drbrain

8:16 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like you didn't pass it through the conversion there.

If you open the image with a text editor, what are its contents?

Does it have "GIF", "JFIF", or "PNG" near the beginning and a bunch of binary characters? You've got a corrupt image of one of those types.

If you've just got a string of letters, numbers, and = signs, then you didn't decode the image.

qlipoth

9:15 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



Yup... you're right... the file is identical to the base64 text in the XML.

One source told me that running it into a byte array, then printing to a file would accomplish the conversion, which made sense, but was WRONG. So, my next question in the series:

How do I make this conversion in VB6? Or should I give up and find a way to borrow a copy of .net for long enough to use the code that sqlgod suggested?

qlipoth

8:07 pm on Aug 24, 2004 (gmt 0)

10+ Year Member



Ahhhh.... after fruitlessly searching the web for days, I finally stumble upon the right combination of words to type into a search engine.

For any who are curious, I used the code that I posted above, replacing the " arrBuffer = oNode.nodeTypedValue " line with:

arrBuffer = objConv.DecodeToByteArray(oNode.nodeTypedValue)

and making use of the class provided in
[paradoxes.info ]

You guys have been a huge help. Thanks.

SychoGent

9:21 pm on Sep 20, 2004 (gmt 0)



Man, I searched and searched for this exact function. It works great thank you.