XForms/上傳
外觀
< XForms
上傳元素允許您使用作業系統的“瀏覽”使用者介面從檔案系統中選擇檔案。然後,該檔案可以傳輸到 Web 表單所在的伺服器。
以下是 Firefox XForms 擴充套件上傳控制元件在選擇 C:\tmp\Foo.xml 檔案後的螢幕影像

<xf:upload ref="XPathExpression" mediatype="text/xml">
<xf:filename ref="XPathExpression" />
<xf:mediatype ref="XPathExpression" />
</xf:upload>
此示例程式具有一個名為“File”的單個例項變數。在選擇結束時,此變數將設定為剛剛選擇的檔案的路徑名。
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<head>
<title/>
<xf:model>
<xf:instance xmlns="">
<Data>
<File xsi:type="xs:anyURI"/>
</Data>
</xf:instance>
</xf:model>
</head>
<body>
<xf:upload ref="/Data/File">
<xf:filename>file:///C:/tmp/*.xml</xf:filename>
<xf:mediatype>text/xml</xf:mediatype>
</xf:upload>
<br/>
<xf:output ref="/Data/File">
<xf:label>File: </xf:label>
</xf:output>
</body>
</html>
在撰寫本文時,關於上傳控制元件如何工作的文件很少。上面的螢幕截圖來自在 Firefox 3 上執行的 Firefox XForms 0.8 實現。請注意,檔名在控制元件本身中出現兩次。這可能不是將來的行為。這可能是因為控制元件內的檔名和媒體型別文字正在被顯示。目前還沒有關於如何停用清除觸發器的文件。
目前還沒有關於如何使用上傳控制元件將例項資料上傳到模型的文件。現在,例項資料必須硬編碼到例項的 src 屬性中。
以下載入影像和 URI。
<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"/>
<uri xsi:type="xs:anyURI"/>
</data>
</xf:instance>
<xf:submission id="save" action="http://xformstest.org/cgi-bin/showinstance.sh" method="post"/>
</xf:model>
</head>
<body>
<xf:upload ref="image">
<xf:label>Upload Photo:</xf:label>
</xf:upload>
<br/>
<xf:upload ref="uri">
<xf:label>Upload File:</xf:label>
<xf:filename>file:///C:/tmp/*.xml</xf:filename>
<xf:mediatype>text/xml</xf:mediatype>
</xf:upload>
<br/>
<xf:output ref="image">
<xf:label>image: </xf:label>
</xf:output>
<br/>
<xf:output ref="uri">
<xf:label>uri: </xf:label>
</xf:output>
<br/>
<xf:submit submission="save">
<xf:label>Save</xf:label>
</xf:submit>
</body>
</html>
任何檔案型別(如影像或 XML 檔案)都可以轉換為 64 位編碼檔案,儲存在例項中,然後在 POST 中傳輸到伺服器。在伺服器上,該檔案可以轉換回字串或二進位制檔案。
以下是使用 XQuery 編寫的簡單回聲指令碼,它以 XML 格式回顯二進位制檔案。它是一個使用二進位制到字串轉換函式的 XQuery 指令碼。它接受一個型別為 xs:base64binary 的檔案,並返回檔案的字串表示形式。要返回的資料在元素名稱為“xml-base64”的元素中。
xquery version "1.0";
declare option exist:serialize "method=xml media-type=text/xml indent=yes";
(: this gets the data from the HTTP POST :)
let $post-data := request:get-data()
(: this converts the base-64 binary to a plaintext string that holds the unparsed XML content :)
let $xml-as-string := util:base64-decode($post-data//xml-base64)
let $parsed-xml := util:parse($xml-as-string)
return
<results>
<xml-as-string>{$xml-as-string}</xml-as-string>
<parsed-xml>{$parsed-xml}</parsed-xml>
</results>
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<head>
<title>Upload XML</title>
<xf:model>
<xf:instance xmlns=''>
<data>
<xml-base64 xsi:type="xs:base64Binary"/>
</data>
</xf:instance>
<xf:submission id='post-to-echo'
action='echo-base64-binary.xq' replace="all"
method='post'/>
</xf:model>
</head>
<body>
<br/>
<xf:upload ref="xml-base64">
<xf:label>Upload XML File:</xf:label>
</xf:upload>
<br/>
<xf:output ref="xml-base64">
<xf:label>XML file encoded in base64binary: </xf:label>
</xf:output>
<br/>
<xf:submit submission="post-to-echo">
<xf:label>Post to Echo</xf:label>
</xf:submit>
</body>
</html>
<test>This is a small XML file.</test>
屬性資料型別分配 xsi:type="xs:anyURI" 必須與用於儲存檔案路徑名的變數相關聯。