Forum Moderators: open
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.
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
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?
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.
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?
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.