Convert PNG to Base64 in ASP

Snitz™ Forums 2000
https://forum.snitz.com/forumTopic/Posts/70611?pagenum=1
04 November 2025, 18:35

Topic


Shaggy
Convert PNG to Base64 in ASP
10 October 2013, 08:05


Greetings, strangers smile Hoping one of you may be able to help me with this little script.
I'm trying to write an ASP script to convert PNG files submitted through a form to Base64 strings without writing the file to the server first.
The HTML for the form is straightforward, as follows:
Code:
<form enctype="multipart/form-data" method="post">
<input type="file" name="file" />
<button type="submit">Convert</button>
</form>
And the code to process it and convert it to Base64 is as follows:
Code:
dim objXml,objStream,binFile
binFile=request.binaryread(request.totalbytes)
set objStream=server.createobject("ADODB.Stream")
objStream.type=1
objStream.open
objStream.write binFile
set objXML=server.createobject("MSXml2.DOMDocument")
set objXml=objXml.createelement("Base64Data")
objXml.datatype="bin.base64"
objXml.nodetypedvalue=objStream.read
response.write replace objXml.text
objStream.close:set objStream=nothing:set objXml=nothing
All works well, except the Base64 string includes the form "header" - Example:
Code:
------WebKitFormBoundarydXk7Jv8nzVuN2zps
Content-Disposition: form-data; name="file"; filename="file.png"
Content-Type: image/png

[File data here]
Any suggestions/pointers for stripping it out at any stage of the process before the encoding takes place? Ideally, I'd also like to read "filename" and "Content-Type" into variables to help me verify that the file is actually a PNG.

 

Replies ...


Carefree
10 October 2013, 10:17


You might try replacing your response.write with something like this:

Code:

If instr(objXml.text,"Content-Type: image/png") AND lCase(right(filename,4))=".png" then
response.Write replace(instr(objXml.text,"Content-Type: image/png")+24)
End If

That SHOULD just start from the position after that line (23 characters of text plus a line feed/carriage return).
Shaggy
10 October 2013, 10:37


Thanks, Carefree, but objXml.text is Base64 encoded binary data so that's not going to work.
Carefree
10 October 2013, 11:04


So objStream is your initial file? OK.
Code:

If instr(objStream,"Content-Type: image/png") AND lCase(right(filename,4))=".png" then
response.Write replace(instr(objXml.text,"Content-Type: image/png")+24)
End If
Shaggy
10 October 2013, 11:10


No, the initial data comes from binFile=request.binaryread(request.totalbytes) and I need to strip out the header data before binFile gets passed to the stream so that all that's going into the stream is the file data.
Carefree
15 October 2013, 15:47


Originally posted by Shaggy
No, the initial data comes from binFile=request.binaryread(request.totalbytes) and I need to strip out the header data before binFile gets passed to the stream so that all that's going into the stream is the file data.

Code:

If instr(binfile,"Content-Type: image/png") AND lCase(right(filename,4))=".png" then
For i = 1 to len(binfile)
If mid(binfile,i,23)="Content-Type: image/png" Then
binfile=mid(binfile,i+24,len(binfile)-i+24)
Exit For
End If
Next
End If
© 2000-2021 Snitz™ Communications