Base64 Encoding File Upload in Classic ASP

05-01-2017

Saving base64 encoding string as file, we can use following codes:

Function SaveToBase64 (base64String)
    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
    call bStream.Open()
    call bStream.Write( nodeB64.NodeTypedValue )
    call bStream.SaveToFile("e:\test.jpg", 2 )
    call bStream.close()
    set bStream = nothing
end function
Function BytesToStr(bytes)
    Dim Stream
    Set Stream = Server.CreateObject("Adodb.Stream")
        Stream.Type = 1 'adTypeBinary
        call Stream.Open
         Stream.Write bytes
        Stream.Position = 0
        Stream.Type = 2 'adTypeText
        Stream.Charset = "iso-8859-1"
        BytesToStr = Stream.ReadText
        call Stream.Close
    Set Stream = Nothing
End Function
        If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
        Dim CanvasStream
         Dim lngBytesCount
        lngBytesCount = Request.TotalBytes
        
        SaveToBase64(BytesToStr(Request.BinaryRead(lngBytesCount)))
        End If
%>

Note: For client side image upload library you can use this plugin: https://github.com/rossturner/HTML5-ImageUploader

© 2019 All rights reserved. Codesenior.COM