ADODB.Stream - 参数类型错误,超出可接受范围或彼此冲突

时间:2014-04-17 19:39:43

标签: vbscript asp-classic adodb

我需要使用经典的asp和vbscript保存图像并在标题中获取错误消息。

我已使用Base64 Encode String in VBScriptConvert hex string (image) to base64 (for browser rendering) in VBScript作为参考点但尚无运气。

我的流程如下。我有一个html 5画布并使用jquery我将图像保存到隐藏字段。

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);

我正在获取数据,我已删除了image.replace(&#39; data:image / png; base64,&#39;,&#39;&#39;)部分,以防出现问题。

我的vbscript代码如下:

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

我甚至尝试了没有XML dom对象的转换,但它继续在以下行中断:

oStream.write imagebinarydata

错误消息:

ADODB.Stream错误&#39; 800a0bb9&#39;参数类型错误,超出可接受的范围,或彼此冲突。

要使用ADODB.Stream,我是否需要安装额外的东西?

应用程序的其他部分(经典的asp和使用vbscript)可以插入和更新记录,并且工作正常。

我也对该文件夹有写权限。

有关寻找什么的想法?

1 个答案:

答案 0 :(得分:0)

问题是您将图像数据编码为Base64两次会破坏它而不是将其从Base64解码为ADODB.Stream可以解释的二进制流。


目前您的流程是;

Base64 String - &gt; Encode to Base64 String - &gt; Build Stream from Base64 String - &gt; Save Stream to File


这个过程应该是;

Base64 String - &gt; Decode to Binary Stream - &gt; Save Stream to File


尝试在SaveFile()中更改此行;

imagebinarydata = Base64Encode(imageData)

imagebinarydata = Base64Decode(imageData)

当然假设您提供的链接中包含Base64Decode()功能。如果没有,你想要这个;

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

<强>链接