How to upload and save a picture with eXist-db?

330 Views Asked by At

I am tryng to upload a picture and store it in exist-db but i get the next error when opening the stored picture:

Cannot open specified file: Could not recognize image encoding.

I have tryed the next code with a small adjustment for normal txt files and it works fine but not with pictures.

picture.xhtml

<?xml-model href="http://www.oxygenxml.com/1999/xhtml/xhtml-xforms.nvdl" schematypens="http://purl.oclc.org/dsdl/nvdl/ns/structure/1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ev="http://www.w3.org/2001/xml-events" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xf="http://www.w3.org/2002/xforms" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <head>
        <title/>
        <xf:model>
            <xf:instance xmlns="">
                <data>
                    <image xsi:type="xs:base64Binary"/>
                </data>
            </xf:instance>
            <xf:submission id="save" action="save.xquery" method="post"/>
        </xf:model>
    </head>
    <body>
        <xf:upload ref="image">
            <xf:label>Upload Photo:</xf:label>
        </xf:upload>
        <br/>
        <xf:submit submission="save">
            <xf:label>Save</xf:label>
        </xf:submit>
    </body>
</html>

save.xquery

xquery version "3.1";

declare option exist:serialize "method=xhtml media-type=text/html indent=yes";

let $login:=xmldb:login('xmldb:exist:///db/apps/places','admin','admin')

(: The small adjusment i refer is just to change file extension from .jpeg to .txt :)
return xmldb:store("/db/apps/places/",concat("pic",".jpeg"), util:base64-decode(request:get-data()//image))
1

There are 1 best solutions below

0
pstadler On

If you want to store images to the eXist-db you should probably replace xmldb:store() with xmldb:store-as-binary().