簡體   English   中英

使用XQL / exist-db將文檔上傳到服務器

[英]Uploading documents to a server using XQL/exist-db

APP結構

NiC
| upload.xq
| upload.html
| controller.xql
| assignments.html (a lot of files are in the main dir, which is probably not good)
| templates/page.html
| inReview/
| data/all the xml files currently in the database
| images/all the images currently in the db
| modules/app.xql
| modules/view.xql

並不是所有的文件都在這里,而是一些相關的文件並帶有某種風味。

當前的新發現和新問題

我一直在努力使這個上傳器正常工作,但我動不動就受阻了-但是,我認為它終於可以工作了。 但是,它的工作方式很怪異,我不知道為什么。 不知道為什么以這種方式而不是其他方式使我發瘋,這將導致其他問題。 所以,我希望有人能有所啟發!

我基本上是從eXist-db中的示例莎士比亞檔案片段中竊取一些新東西。 因此,我整個應用程序的結構實際上與附帶的演示程序相同。 話雖如此:

在page.html中,該模板將頁面的整體外觀模板化,並提供標題/導航信息:

我已經添加了指向上載頁面的導航的鏈接:

<li><a href="../upload.html">Contribute</a></li>

upload.html:

<div xmlns="http://www.w3.org/1999/xhtml" class="templates:surround?with=templates/page.html&amp;at=main">
        <form enctype="multipart/form-data" method="post" action="upload.xql">
            <fieldset>
            <legend>Upload an XML File for Review:</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>

upload.xql:

xquery version "3.0";

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, 'username', 'superstrongpassword')
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>

所有這些都以某種方式起作用。 但是奇怪的是在controller.xql中:

這是相關的位:

else if ($exist:resource = ("search.html", "form1.html", "demo-queries.html", "search-help.html", "about-NiC.html", "assignments.html", "success.html", "upload.html")) then
    (: the html page is run through view.xql to expand templates :)
    (: question: 11/2017 why if upload.html is noted here does the URL need to be relative to ../upload.html from page.html, though others don't? :)
    <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
        <forward url="{$exist:controller}/{$exist:resource}">
            <set-header name="Cache-Control" value="no-cache"/>
        </forward>
        <view>
            <forward url="{$exist:controller}/modules/view.xql"/>
        </view>
        <error-handler>
            <forward url="{$exist:controller}/error-page.html" method="get"/>
            <forward url="{$exist:controller}/modules/view.xql"/>
        </error-handler>
    </dispatch>

如果在page.html中,我僅使用控制器似乎需要的鏈接“ upload.html”(upload.html與其他頁面在同一位置,並且通常在做其他頁面一樣的事情,例如assignments.html)依此類推)無效。 我收到序言中不允許的SAX異常錯誤/內容。 其他頁面可訪問,並且URL解析按預期工作。 但是,如果鏈接為“ ../upload.html”,則可以訪問該頁面,並且可以按照我的要求進行上傳。

所以我的問題是:為什么? 為什么我不能僅僅使用upload.html作為路徑來信任控制器來完成它的工作? 為什么我必須通過相對路徑來解決問題?

啊!

request:get-uploaded-file-data()的返回類型為xs:base64Binary*xmldb:store()期望“節點,xs:string,Java文件對象或xs:anyURI”(來自功能文檔)。 因此,您必須首先通過util:binary-to-string()結果

xmldb:store($collection, $filename, util:binary-to-string(request:get-uploaded-file-data('file')))

您看到的錯誤是因為xmldb:store()如果不成功,則會拋出XPathException。

因此,僅是澄清您​​的應用程序結構是什么?

yourapp
| modules/upload.xq
| templates/page.html
| upload.html
| other1.html
| controller.xql

您的other1.html文件還會調用.xq模塊嗎? 控制器對.html和.xq文件的處理方式不同,因此要從modules / upload.xq進行操作,您需要使用相對路徑步驟../才能到達upload.html。

話雖這么說,您並不孤單,常常會因模板系統對當前情況的看法而感到愚蠢:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM