user2029541
user2029541

Reputation: 666

Save Base64 to an image using Classic ASP

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

Upvotes: 2

Views: 11392

Answers (3)

user2029541
user2029541

Reputation: 666

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

Upvotes: 2

ulluoink
ulluoink

Reputation: 2785

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

Upvotes: 3

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114347

You cannot do this due to security reasons. If web pages could randomly choose where to store files on our local systems without any user interaction, there would chaos.

Upvotes: -3

Related Questions