2014-10-17 20 views
-2

我對CSS(3)和HTML(5)非常瞭解,但是我目前的項目允許我進一步研究。然而,這對我來說是完全未知的地形。在我的項目中,我有一個textarea,用戶可以在其中提交一些XML,然後使用jQuery的$.parseXML方法進行解析。但是,我想添加上載XML文件的功能。讓用戶上傳一個xml文件並解析它(不含PHP)?

我想上傳按鈕應該是這樣的:

<form name="upload-form" action="???" method="???"> 
    <input type="file" name="upload-field"> 
</form> 

不過,我不知道如何尋找的動作和方法應該像。因爲我留在同一頁面上,並且沒有什麼特別的事情發生,所以我猜測動作屬性可以被忽略?方法屬性可能是get,而不是post(1)

然後呢?我不知道如何從jQuery解析器中上傳的XML文檔中獲取數據。 (2)另外,如何檢查正確的文件類型?這發生服務器端? (3)

+0

恐怕你不能使用客戶端語言上傳文件。如果你使用jquery驗證文件類型(假設可能),每個人都可以通過停用javascript來克服控制。我強烈建議你使用服務器端語言來做到這一點。 – Stubborn 2014-10-17 17:43:58

+1

查看[FileReader API](https://developer.mozilla.org/en-US/docs/Web/API/FileReader),[使用來自Web應用程序的文件](https://developer.mozilla。 org/en-US/docs/Using_files_from_web_applications)和[this question](http://stackoverflow.com/questions/750032/reading-file-contents-on-the-client-side-in-javascript-in-various -browsers)。根據您需要支持的瀏覽器,這看起來是可行的。 – cOle2 2014-10-17 17:53:41

+0

我已經關閉了第一個問題的重複 - 呃問題:上傳文件停留在同一個網站上。除此之外,Stackoverflow對於一個明確的問題陳述(或者我經常說的話:一次只有一個問題)的效果最好。 – hakre 2014-10-18 15:34:46

回答

0

action包含將接收表單數據的服務器端腳本的名稱。當使用post時,瀏覽器不會將輸入字段的內容解析到url中。服務器端腳本,例如 。在php中會收到請求,做安全檢查並保存數​​據。

<form name="upload-form" action="serversidescript.php" method="post"> 
    <input type="file" name="upload-field"> 
    <input type="submit" value="Submit"> 
</form> 

serversidescript.php

<pre> 
<?php 
print_r($_REQUEST); // shows the array containing the form data 
0

離開法空,使用POST方法。爲文件使用文章很重要,因爲它具有安全功能並允許您上傳更多數據。

因爲它上傳您需要添加ENCTYPE,ENCTYPE =「的multipart/form-data的」一號文件或將無法正常工作

<form name="upload-form" action="" method="post" enctype="multipart/form-data" > 
    <input type="file" name="upload-field"> 
    <input type="submit" value="Submit"> 
</form> 

,然後解析文件,你需要閱讀xml文件使用jQuery,然後根據你需要做的事情編寫一個解析器函數。

//write the custome parse function 
function parse(data){ 
    //code for your parse function goes here. 
    // to append the file to html, I need the actual structure of the file to show you 
} 

//this reads the xml file using jquery 
$.ajax({ 
    url: 'name.xml', // name of file you want to parse 
    dataType: "xml", 
    success: parse, //this calls the parse function 
     error: function(){alert("Error: Something went wrong");} 
    }); 
相關問題