Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 Convert PNG to Base64 in ASP
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Shaggy
Support Moderator

Ireland
6780 Posts

Posted - 10 October 2013 :  08:05:31  Show Profile  Reply with Quote
Greetings, strangers 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:
<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:
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:
------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.”

Carefree
Advanced Member

Philippines
4207 Posts

Posted - 10 October 2013 :  10:17:15  Show Profile  Reply with Quote
You might try replacing your response.write with something like this:


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).

Edited by - Carefree on 10 October 2013 10:20:27
Go to Top of Page

Shaggy
Support Moderator

Ireland
6780 Posts

Posted - 10 October 2013 :  10:37:05  Show Profile  Reply with Quote
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.”
Go to Top of Page

Carefree
Advanced Member

Philippines
4207 Posts

Posted - 10 October 2013 :  11:04:35  Show Profile  Reply with Quote
So objStream is your initial file? OK.


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
Go to Top of Page

Shaggy
Support Moderator

Ireland
6780 Posts

Posted - 10 October 2013 :  11:10:56  Show Profile  Reply with Quote
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.”
Go to Top of Page

Carefree
Advanced Member

Philippines
4207 Posts

Posted - 15 October 2013 :  15:47:58  Show Profile  Reply with Quote
quote:
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.






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
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.21 seconds. Powered By: Snitz Forums 2000 Version 3.4.07