XQuery/上傳檔案
外觀
< XQuery
您希望使用簡單的 HTML 表單將檔案上傳到您的 eXist 資料庫。
我們將使用 HTML <input> 元素在網頁表單中,以及在 XQuery 中使用 store 函式。
我們將使用標準的 HTML 表單,但我們將新增一個 enctype="multipart/form-data" 屬性。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML Upload</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="upload.xq">
<fieldset>
<legend>Upload Document:</legend>
<input type="file" name="file"/>
<input type="submit" value="Upload"/>
</fieldset>
</form>
</body>
</html>
在伺服器端,我們將使用 request:get-uploaded-file-name() 獲取傳入檔案的名稱,並使用 request:get-uploaded-file-data() 函式獲取檔案資料。然後,我們可以使用 xmldb:store() 函式儲存檔案。
檔案: upload.xq
let $collection := '/db/test/upload-test'
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, 'admin', 'my-admin-password')
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>
此示例是由 Rémi Arnaud 於 2010 年 11 月 5 日在 eXist 開放郵件列表中釋出的。
