2

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

3 Answers 3

3

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
6
  • 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 Commented Jan 31, 2013 at 15:34
  • @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!
    – tamak
    Commented Sep 18, 2014 at 1:13
  • 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 Commented Sep 18, 2014 at 10:40
  • @user2029541 - awesome... exactly what Im looking for, and thanks! I'll check back tomorrow!
    – tamak
    Commented Sep 18, 2014 at 11:11
  • @user2029541 - please remember to share that code when you have a moment and thanks again!
    – tamak
    Commented Sep 19, 2014 at 12:26
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
0
-3

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.

1
  • 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 Commented Jan 31, 2013 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.