2013-05-23 18 views
19

HTML5ファイルAPIを使用してファイルを読み取るためのコードは次のとおりです。 input = file要素を介してファイルをアップロードしました。以下はコードブロックです。ファイル名のパスを持つHTML5ファイルAPI

<input type="file" id="files" name="file" /> 
<button id="readFile">Read File</button> 
<output id="content"></output> 

<script> 

function readFile() 
{ 
    /* Get the reference of the inpout element. */ 
    var files = document.getElementById('files').files; 
    console.log(files); 

    if (!files.length) 
    { 
     alert('Please select a file!'); 
     return; 
    } 

    /* Reading the first file selected. You can process other files similarly in loop. */ 
    var file = files[0]; 

    /* Instantiate the File Reader object. */ 
    var reader = new FileReader(); 

    /* onLoad event is fired when the load completes. */ 
    reader.onload = function(event) { 
     document.getElementById('content').textContent = event.target.result;  
    }; 

    /* The readAsText method will read the file's data as a text string. By default the string is decoded as 'UTF-8'. */ 
    reader.readAsText(file); 
} 

document.getElementById('readFile').addEventListener('click', function(event) { 
    readFile(); 
    }, false); 

</script> 

私は、ファイルをアップロードし、HTML5への入力=タイプ要素を介してファイルパスを提供したくない場合はどう:ファイルAPIファイルを読み込み、それを表示するには?

私はHTML5:File APIが直接ファイルパスを取っていないことを知っています。解決策はありますか?

+0

[ファイル入力からファイルのフルパスを取得する方法](http://stackoverflow.com/questions/4176377/how-to-get-the-full-path -of-the-file-from-a-file-input) – Quentin

+0

まだパスを取得できませんが、ファイルの内容を取得できますか? – HMR

+1

みんな、input = fileからパスを取得するよう求めていません。 私はすでにいくつかの変数にファイルパスを持っています。私はちょうどそのファイルパスをHTML5で使用したい:ファイルAPIは、その尊重されたファイルを読んで、いくつかのdivまたはテキストエリアにそのコンテンツを表示する... 私は私の質問にいくつかの変更を加えましょう。 –

答えて

17

セキュリティ上の理由から、ブラウザでは絶対パス&ファイルシステムをJavascriptに直接アクセスすることはできません。あなたはjavascriptの中の 'val()'関数を呼び出すだけでファイル名を得ることができます。

時間を無駄にしないでください。

0

IEの最新バージョンは、テキストボックスにファイルの完全な絶対パスを返します。 var filePath = document.getElementById( "myFile")。value;使用することができます。 絶対パスを取得する

P.S. IE 11でのみ試した

+0

最新のchrome/firefox/safariで試したことはありますか?私はまだ道を得ることができますか? – chovy

+0

復帰偽のパス.............. –

関連する問題