1

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?

Community
  • 1
  • 1
Sonja
  • 45
  • 2
  • 7
  • Do a msgbox vartype(imagedata) and msgbox IsArray(imagedata) msgbox IsDate(imagedata) msgbox IsEmpty(imagedata) msgbox IsNull(imagedata) msgbox IsNumeric(imagedata) msgbox IsObject(imagedata) and also for imagebinarydata – tony bd Apr 17 '14 at 23:05
  • You're mixing up your encoding, if you want to send an image in `Base64` that is fine but at the [tag:asp-classic] end you want to decode it not encode it again, it is already `Base64` what you want is a binary stream (this is why `ADODB.Stream` is complaining that the argument is the wrong type). Using `Base64Decode()` instead of `Base64Encode()` should give you what you need. – user692942 Apr 18 '14 at 11:56
  • I've added an answer that should explain better. – user692942 Apr 18 '14 at 12:23
  • @Tony bd this is the response. I am able to save the image now, its just corrupt now. – Sonja Apr 18 '14 at 17:05
  • @Tony bd ****vartype : 8 ****IsArray : False ****IsDate : False ****IsEmpty : False ****IsNull : False ****IsNumeric : False ****IsObject : True ****imagebinarydata : success – Sonja Apr 18 '14 at 17:05
  • @Lankymart, yes I have tried without having to encode it again to base64, but this was my last attempt to get the file saving – Sonja Apr 18 '14 at 17:06
  • @Sonja you're missing the point, if that is the code you are using it will never work see the comparison of your process versus what you should be doing in my answer. – user692942 Apr 18 '14 at 17:11

1 Answers1

0

The issue is you are encoding the image data to Base64 twice which will corrupt it rather than decoding it from Base64 to a binary stream that ADODB.Stream can interpret.


At the moment your process is;

Base64 String -> Encode to Base64 String -> Build Stream from Base64 String -> Save Stream to File


The process should be;

Base64 String -> Decode to Binary Stream -> Save Stream to File


Try changing this line in SaveFile() from;

imagebinarydata = Base64Encode(imageData)

to;

imagebinarydata = Base64Decode(imageData)

Assuming of course you have the Base64Decode() function from the links you provided. If not you want this;

Function Base64Decode(ByVal vCode)
    Dim oXML, oNode

    Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
    Set oNode = oXML.CreateElement("base64")
    oNode.dataType = "bin.base64"
    oNode.text = vCode
    Base64Decode = Stream_BinaryToString(oNode.nodeTypedValue)
    Set oNode = Nothing
    Set oXML = Nothing
End Function

Links

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175
  • I've changed the code as suggested and I can save the file. When I try to open this file it says its corrupt though. The image is about 10kb so its not empty. – Sonja Apr 18 '14 at 17:12
  • @Sonja Can you post the updated code in your original question please? – user692942 Apr 22 '14 at 08:43