Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Convert PNG to Base64 in ASP

  • 10-10-2013 12:01pm
    #1
    Registered Users, Registered Users 2 Posts: 242 ✭✭


    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 code 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
    
    [I][File data here][/I]
    

    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.


Comments

  • Moderators, Society & Culture Moderators Posts: 17,643 Mod ✭✭✭✭Graham


    Can you add a content type? I think it's something like:

    response.ContentType="image/PNG"

    Ignore that, just spotted the content-type is already detected.


  • Registered Users, Registered Users 2 Posts: 242 ✭✭MeTV


    To clarify: I need to extract just the file data from the submitted data - e.g., just [File data here] from the third code box above.


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    I'm just going from a vague memory here, but I think the Response object can add headers in and I'm guessing that is possibly what is happening here. To test this quickly you could try writing out to a file instead of returning it via Response, just to check that it looks ok there.

    From what I remember there are various properties on the Response object that you can set to control what headers are sent, so you should be able to use those to disable the headers entirely.


  • Registered Users, Registered Users 2 Posts: 242 ✭✭MeTV


    Saved the stream to a text file and the header was included.


  • Moderators, Society & Culture Moderators Posts: 17,643 Mod ✭✭✭✭Graham


    Are you outputting the object itself rather than just a string containing the converted image?


  • Advertisement
Advertisement