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が直接ファイルパスを取っていないことを知っています。解決策はありますか?
[ファイル入力からファイルのフルパスを取得する方法](http://stackoverflow.com/questions/4176377/how-to-get-the-full-path -of-the-file-from-a-file-input) – Quentin
まだパスを取得できませんが、ファイルの内容を取得できますか? – HMR
みんな、input = fileからパスを取得するよう求めていません。 私はすでにいくつかの変数にファイルパスを持っています。私はちょうどそのファイルパスをHTML5で使用したい:ファイルAPIは、その尊重されたファイルを読んで、いくつかのdivまたはテキストエリアにそのコンテンツを表示する... 私は私の質問にいくつかの変更を加えましょう。 –