Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 Convert PNG to Base64 in ASP

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!
Before posting, make sure you have read this topic!

Screensize:
UserName:
Password:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkInsert EmailInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
   

T O P I C    R E V I E W
Shaggy Posted - 10 October 2013 : 08:05:31
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.

5   L A T E S T    R E P L I E S    (Newest First)
Carefree Posted - 15 October 2013 : 15:47:58
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
Shaggy Posted - 10 October 2013 : 11:10:56
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 Posted - 10 October 2013 : 11:04:35
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
Shaggy Posted - 10 October 2013 : 10:37:05
Thanks, Carefree, but objXml.text is Base64 encoded binary data so that's not going to work.

Carefree Posted - 10 October 2013 : 10:17:15
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).

Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000 Version 3.4.07