Convert PNG to Base64 in ASP - Postet den (1769 Views)
Support Moderator
Shaggy
Innlegg: 6780
6780
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.
Search is your friend “I was having a mildly paranoid day, mostly due to the
fact that the mad priest lady from over the river had
taken to nailing weasels to my front door again.”
   
 Sidestørrelse 
Postet den
Advanced Member
Carefree
Innlegg: 4224
4224
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).
Postet den
Support Moderator
Shaggy
Innlegg: 6780
6780
Thanks, Carefree, but objXml.text is Base64 encoded binary data so that's not going to work.
Search is your friend “I was having a mildly paranoid day, mostly due to the
fact that the mad priest lady from over the river had
taken to nailing weasels to my front door again.”
Postet den
Advanced Member
Carefree
Innlegg: 4224
4224
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
Postet den
Support Moderator
Shaggy
Innlegg: 6780
6780
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.
Search is your friend “I was having a mildly paranoid day, mostly due to the
fact that the mad priest lady from over the river had
taken to nailing weasels to my front door again.”
Postet den
Advanced Member
Carefree
Innlegg: 4224
4224
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
 
Du må legge inn en melding