Save Base64 to an image using Classic ASP

10,129

Solution 1

use an adodb.stream object to store the image on the server side like so:

dim bStream : set bStream = server.CreateObject("ADODB.stream")

bStream.type = adTypeBinary

call bStream.Open()

call bStream.Write( binData )

call bStream.SaveToFile( FullName, adSaveCreateOverWrite)

call bStream.close()
set bStream = nothing

Solution 2

The server side code that receives the base64 string is below, please note that this is code that is taken from a working system so there are variables such as carreg / auditdate that are used as unique identifiers for giving the created file a name:

function convBase64 (convVal, getCarReg, convType, AuditDate, AuditReference)
    base64String = convVal
    carReg = (UCase(getCarReg))
    carReg = (Replace(getCarReg," ",""))

    AuditDate= CDate(AuditDate) 
    ConvAuditDate = ((DatePart("d",AuditDate))& "_" & (DatePart("m",AuditDate)) & "_" & (DatePart("YYYY",AuditDate)))

    select case convType
        Case "Sig1"
        FileNameSuffix = "AuditorsSignature"
        Case "Sig2"
        FileNameSuffix = "BodyShopSignature"
        Case "Car"
        FileNameSuffix = "DamageCanvas"
    end select
    ImageFileName =  FileNameSuffix & "-" & carReg & "-" & ConvAuditDate & ".jpg"

        Set tmpDoc = Server.CreateObject("MSXML2.DomDocument")
        Set nodeB64 = tmpDoc.CreateElement("b64")
        nodeB64.DataType = "bin.base64" ' stores binary as base64 string
        nodeB64.Text = Mid(base64String, InStr(base64String, ",") + 1) ' append data text (all data after the comma)


        dim bStream : set bStream = server.CreateObject("ADODB.stream")
        bStream.type =  1
        call bStream.Open()
        call bStream.Write( nodeB64.NodeTypedValue )
        call bStream.SaveToFile(Server.Mappath("NoneVehicleImages/" & AuditReference & "/" &  ImageFileName), 2 )
        call bStream.close()
        set bStream = nothing
        convBase64 = "\\iis_fdg$\AuditExport\NoneVehicleImages\"  & AuditReference & "\" & ImageFileName
end function
Share:
10,129
user2029541
Author by

user2029541

Updated on June 04, 2022

Comments

  • user2029541
    user2029541 almost 2 years

    I have been trying to save a base64 file as an image from server side using classic ASP. What I want is it to autosave the file to a specific location and give it a filename, Now I am fine coding that aspect of it. However I can't get the code to save the image without first rendering on a browser. This isn't going to work for me as the script I am using will be an automatic export and have no user input.

    Code follows as yet that renders in the webpage and asks the user where to save the image. Just to reiterate I need it to auto save (no user input)

    base64String ="base64 code goes here - Wont add it as its huge amount of text"
    
    Set tmpDoc = Server.CreateObject("MSXML2.DomDocument")
    Set nodeB64 = tmpDoc.CreateElement("b64")
    nodeB64.DataType = "bin.base64" ' stores binary as base64 string
    nodeB64.Text = Mid(base64String, InStr(base64String, ",") + 1) ' append data text (all data after the comma)
    
    vehicleAuditName= "Audit1"
    
    With Response
       .Clear
       .ContentType = "image/png"
       .AddHeader "Content-Disposition", "attachment; filename=" & vehicleAuditName & ".png"
       .BinaryWrite nodeB64.NodeTypedValue 'get bytes and write
       .end
    End With
    
  • user2029541
    user2029541 about 11 years
    thanks for you reply, I have done this dynamically with images, so I know its possible, just not from base64 images, so I need the code to convert and save the base64 as an image
  • user2029541
    user2029541 about 11 years
    Thanks for your reply I am not sure how I would use this in the context of my above code (coverting the base 64 and then passing in the image to be saved). Could you explain or show a full example. Thanks again
  • tamak
    tamak over 9 years
    @user2029541 - can you show me how you got this to work? Im also trying get ASP / VBscript to save base64 to an image file on the server. - Thanks!
  • user2029541
    user2029541 over 9 years
    Hi Tamak, I am not in work today where the code is located, I will dig it out and post it tomorrow. However its in classic ASP not VB.net
  • tamak
    tamak over 9 years
    @user2029541 - awesome... exactly what Im looking for, and thanks! I'll check back tomorrow!
  • tamak
    tamak over 9 years
    @user2029541 - please remember to share that code when you have a moment and thanks again!
  • user2029541
    user2029541 over 9 years
    Hi Tamak I have posted the solution below - hope it helps