2017-02-04 174 views
0

我試圖在Chrome上獲取文件路徑時遇到問題。這是我的代碼有的JavaScript功能:在Chrome瀏覽器上獲取文件路徑

<html> 
<head> 
<script language="javascript" type="text/javascript"> 
function getPath() { 
var inputName = document.getElementById('ctrl'); 
var imgPath; 

imgPath = inputName.value; 
//alert(imgPath); 
var x = document.createElement("INPUT"); 
x.setAttribute("type", "text"); 
x.setAttribute("value", imgPath); 
document.body.appendChild(x); 
} 
</script> 
</head> 
<body> 


<input type="file" name ="file1" id="ctrl" webkitdirectory directory multiple/> 
<input type="submit" value="Enviar" onclick="getPath()"/> 

</body> 
</html> 

當我按下按鈕,文本字段顯示此

C:\fakepath\ART.pdf 

我嘗試過編輯Internet Explorer設置,它工作正常,但我無法獲得鉻的完整路徑 它的任何方式獲得完整路徑?由於

+0

你爲什麼需要這條路? – epascarello

+0

允許用戶選擇將要保存一些文件的目錄 –

+0

出於明顯的安全原因,您無法訪問它。你想讓任何網站能夠看到你的目錄結構嗎? – charlietfl

回答

0

您正在使用的多重目錄選項,以便您的路徑將是avaible inputName.files

下,出於安全原因,這些將是相對路徑。如果你想閱讀文件,你需要使用FileReader API。

https://developer.mozilla.org/en-US/docs/Web/API/FileReader

我在您嘗試將文件保存到特定位置的評論閱讀。這不能通過瀏覽器來實現。您只需提供要下載的文件,用戶將決定將其保存到哪裏。通常,它將被保存到操作系統的默認下載文件夾中。

+0

但fileReader API does not幫助我得到完整的路徑。它也顯示我與假路徑 –

相關問題