0

All of the files are available at http://gist.github.com/tonyahowe -- page.html, app.xql, upload.html, and form1.html. The app:upload function in appxql is at line 378.

I am trying to add an upload feature to my application here (http://nic.cerosia.org) and have been able to get the upload to work without integrating it into the whole application. Once I integrate it, I get a 400 error message, included below. I think the problem might be a controller issue, but I'm not sure?

I have added a simple link from the index to upload.html page, as follows:

<div xmlns="http://www.w3.org/1999/xhtml" data-template="templates:surround" data-template-with="templates/page.html" data-template-at="main">
        <form enctype="multipart/form-data" method="post" action="form1.html">
            <fieldset>
                <legend>Upload Document:</legend>
                <input type="file" name="file"/>
                <button id="f-btn-upload" name="f-btn-upload" value="true" type="submit" class="btn btn-danger">Upload</button>
            </fieldset>
        </form>

</div>

That sends the upload file to form1.html, which basically figures out what to do with different form actions (it works with the first two options):

<div xmlns="http://www.w3.org/1999/xhtml" data-template="templates:surround" data-template-with="templates/page.html" data-template-at="main">
    <div/>
    
    <!-- if a query search -->
    <div data-template="templates:if-parameter-set" data-template-param="query">
        <div data-template="templates:include" data-template-path="search.html" class="col-md-12"/>
    </div>
    
    <!-- if a coursepack search -->
    <div data-template="templates:if-parameter-set" data-template-param="f-btn-coursepack">
        <div data-template="templates:include" data-template-path="coursepack.html" class="col-md-12"/>
    </div>
    
    <!-- if an upload  -->
    <div data-template="templates:if-parameter-set" data-template-param="f-btn-upload">
        <div data-template="templates:include" data-template-path="success.html" class="col-md-12"/>
        <div data-template="app:upload"/>
    </div> 
</div>

The app:upload is here:

declare function app:upload($node as node(), $model as map(*), $type as xs:string?) {
   
let $collection := '/db/apps/NiC/inReview/'
let $filename := request:get-uploaded-file-name('file')

(: make sure you use the right user permissions that has write access to this collection :)
let $login := xmldb:login($collection, 'public', 'public')
let $store := xmldb:store($collection, $filename, request:get-uploaded-file-data('file'))

return
<results>
   <message>File {$filename} has been stored at collection={$collection}.</message>
</results>

};

I keep getting an error that I think has something to do with the controller, but I'm not sure. Can anyone help me figure out where the problem is? It may also be a templating issue?

Here's the error:

HTTP ERROR 400

Problem accessing /exist/apps/NiC/works/form1.html. Reason:

SAX exception while parsing request: Content is not allowed in prolog.
Tonya Howe
  • 57
  • 5

1 Answers1

0

I don't know for sure, but I wonder if it is an issue with the templating framework reading the data from the request body and then it not being available in your app:upload function.

I wonder if we could test this, perhaps instead of checking for f-btn-upload which will be in the body of the HTTP request, you could instead check for a query-string parameter. If you change your form in the upload.html page to be like this:

<form enctype="multipart/form-data" method="post" action="form1.html?do-upload=true">

and the switch in your form1.html to be like this:

 <div data-template="templates:if-parameter-set" data-template-param="do-upload">

Does that help you at all?

adamretter
  • 3,885
  • 2
  • 23
  • 43
  • Hi, Adam! I made the changes you suggested, and I'm getting the same error. It's odd--the form1.html page works as intended, of course, for the first two options, but I'm not getting an error internal to exist-db, it seems. – Tonya Howe Feb 05 '17 at 16:08
  • Okay Tonya, is the code available so that I can try and reproduce and debug the issue here? – adamretter Feb 08 '17 at 20:11
  • Yes! I'll post it all on gist and then send a note here. All help is wonderfully appreciated. – Tonya Howe Feb 16 '17 at 19:03
  • All the files are here: https://gist.github.com/tonyahowe -- page.html, app.xql, upload.html, and form1.html. I think that's it. The app:upload function is at line 378. – Tonya Howe Feb 16 '17 at 19:09