经典ASP Base64,image / png - >另存为图像

时间:2016-04-08 09:11:06

标签: asp-classic

这个特殊的问题已被提出并回答,但无论我尝试什么,我都无法让它发挥作用。在这一点上,我有点准备把我的电脑扔到窗外..

无论我尝试什么组合,它仍然会失败:

oStream.write imagebinarydata   

以下是带注释的代码:

sFileName = Server.MapPath("grafer/test.png")
ByteArray = Request.Form("imageData")
ByteArray = [DATA-URI String] 'This string shows the image perfectly fine, in an image tag in the top of the page so it should be perfectly ok?

response.write ("Decoded: " & Base64Decode(ByteArray)) '<- Writes 'PNG' ?


Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2

Set oStream = Server.CreateObject("ADODB.Stream")

oStream.type = adTypeBinary
oStream.open 
imagebinarydata = Base64Decode(ByteArray) 
oStream.write imagebinarydata                                   '<- FAILS

'Error: 

'ADODB.Stream error '800a0bb9'

'Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.

'Use this form to overwrite a file if it already exists
oStream.savetofile sFileName, adSaveCreateOverWrite

oStream.close

set oStream = nothing

response.write("success")

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

Function Stream_BinaryToString(Binary)

    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 = adTypeBinary

    'Open the stream And write text/string data To the object
    BinaryStream.Open

    BinaryStream.Write Binary 

    'Change stream type To binary
    BinaryStream.Position = 0
    BinaryStream.Type = adTypeText

    'Specify charset For the source text (unicode) data.
    If Len(CharSet) > 0 Then
    BinaryStream.CharSet = CharSet
    Else
    BinaryStream.CharSet = "us-ascii"
    End If

    'Open the stream And get binary data from the object
    Stream_BinaryToString = BinaryStream.ReadText

End Function

1 个答案:

答案 0 :(得分:2)

如果您要保存,可以使用此功能

function SaveToBase64 (base64String)
    ImageFileName = "test.jpg"

    Set Doc = Server.CreateObject("MSXML2.DomDocument")
    Set nodeB64 = Doc.CreateElement("b64")
    nodeB64.DataType = "bin.base64"
    nodeB64.Text = Mid(base64String, InStr(base64String, ",") + 1)

    dim bStream
    set bStream = server.CreateObject("ADODB.stream")
    bStream.type =  1
    bStream.Open()
    bStream.Write( nodeB64.NodeTypedValue )
    bStream.SaveToFile(Server.Mappath("Images/" & ImageFileName), 2 )
    bStream.close()
    set bStream = nothing
end function