I need to save an image using classic asp and vbscript and getting the error message in the title.
I've used both Base64 Encode String in VBScript and Convert hex string (image) to base64 (for browser rendering) in VBScript as reference points but no luck yet.
My process is as follows. I have a html 5 canvas and using jquery I am saving the image to a hiddenfield.
html :
<input type="hidden" id="imageData" name="imageData">
jquery :
var image = document.getElementById("canvas").toDataURL("image/png");
image = image.replace('data:image/png;base64,', '');
$('#imageData').val(image);
I am getting data and I have removed the image.replace('data:image/png;base64,', '') section in case that is the problem.
My vbscript code is as follows :
Function SaveFile(imageData)
dim fs,f,mappedpath,filename, userid, fullpathandfilename, imagebinarydata, oStream
userid = 12
filename = Month(now())&"_"&Day(now())&"_"&Year(now())&"_"&Minute(now())&"_"&Second(now())&".png"
mappedpath = Server.MapPath("images/")
fullpathandfilename = mappedpath + "\" + filename
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Set oStream = Server.CreateObject("ADODB.Stream")
oStream.type = adTypeBinary
oStream.open
imagebinarydata = Base64Encode(imageData)
oStream.write imagebinarydata
'Use this form to overwrite a file if it already exists
oStream.savetofile fullpathandfilename, adSaveCreateOverWrite
oStream.close
set oStream = nothing
response.write("success")
End Function
Function Base64Encode(sText)
Dim oXML, oNode
Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
Set oNode = oXML.CreateElement("base64")
oNode.dataType = "bin.base64"
oNode.nodeTypedValue =Stream_StringToBinary(sText)
Base64Encode = oNode.text
Set oNode = Nothing
Set oXML = Nothing
End Function
'Stream_StringToBinary Function
'2003 Antonin Foller, http://www.motobit.com
'Text - string parameter To convert To binary data
Function Stream_StringToBinary(Text)
Const adTypeText = 2
Const adTypeBinary = 1
'Create Stream object
Dim BinaryStream 'As New Stream
Set BinaryStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To save text/string data.
BinaryStream.Type = adTypeText
'Specify charset For the source text (unicode) data.
BinaryStream.CharSet = "us-ascii"
'Open the stream And write text/string data To the object
BinaryStream.Open
BinaryStream.WriteText Text
'Change stream type To binary
BinaryStream.Position = 0
BinaryStream.Type = adTypeBinary
'Ignore first two bytes - sign of
BinaryStream.Position = 0
'Open the stream And get binary data from the object
Stream_StringToBinary = BinaryStream.Read
Set BinaryStream = Nothing
End Function
I even tried the conversion without the XML dom object but it keeps on breaking at the following line :
oStream.write imagebinarydata
with error message:
ADODB.Stream error '800a0bb9' Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
To work with ADODB.Stream do I have to install anything extra?
There are other parts to the application (classic asp and using vbscript) that inserts and updates records and that is working properly.
I also do have write permissions on that folder.
Any ideas on what to look for?