使用Classic ASP将Base64保存到图像

时间:2013-01-31 15:18:59

标签: asp-classic

我一直在尝试使用经典ASP将base64文件从服务器端保存为图像。我想要的是将文件自动保存到一个特定的位置,并给它一个文件名,现在我很好地编码它的方面。但是,如果没有首先在浏览器上呈现,我无法获取保存图像的代码。这对我来说不起作用,因为我使用的脚本将是自动导出并且没有用户输入。

代码随后会在网页中呈现并询问用户保存图像的位置。重申我需要它来自动保存(无用户输入)

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 个答案:

答案 0 :(得分:3)

使用adodb.stream对象将图像存储在服务器端,如下所示:

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

答案 1 :(得分:2)

接收base64字符串的服务器端代码如下所示,请注意这是从工作系统获取的代码,因此有一些变量如carreg / auditdate用作唯一标识符,用于为创建的文件命名:

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

答案 2 :(得分:-3)

出于安全原因,您无法执行此操作。如果网页可以随机选择在我们的本地系统上存储文件的位置而无需任何用户交互,那么就会出现混乱。