獲得input type="file" (由用戶在文件對話框中選擇的條目)如何在JSP
<script>
function OpenFileDialog(form) {
var a = document.getElementById("inputfile").click();
SampleForm.filePath.value = //set the path here
document.SampleForm.submit();
}
</script>
<form name="SampleForm" action="TestServlet" method="get">
<input type="file" style="display:none;" id="inputfile"/>
<a href="javascript:OpenFileDialog(this.form);">Open here</a>
<input type="hidden" name="filePath" value=""/>
</form>
我的文件路徑希望在我的Servlet類中讀取所選文件的路徑 如何獲取文件路徑?我可以從var a
讀取嗎? 或者有什麼方法可以直接從我的servlet中的input type="file"
訪問文件路徑?
感謝BalusC爲您的詳細說明.. :) 我是新來的,因此不理解訪問文件路徑的影響... 我想我需要使用Servlet到版本2.5,所以我會按照你提到的方法閱讀文件的內容... 再次感謝! – deepthinker121 2010-04-07 13:05:22
不,servlet版本沒有限制。 2.5版本以前沒有內置的方法可以輕鬆解析上傳的文件。您需要獲取Apache Commons FileUpload。但是,如果您已經使用全新的3.0版本,那麼您可以使用內置的方式來解析上傳的文件。 – BalusC 2010-04-07 13:07:21
好吧,明白了.. 我也想執行反向操作 - 從服務器下載文件 - 想要打開文件對話框來選擇下載位置......這怎麼辦? – deepthinker121 2010-04-07 17:52:38