jqueryでローカルファイル(テキストファイル)の読込み
- <!DOCTYPE html>
- <html lang="ja">
- <head>
- <meta charset="UTF-8">
- <title>jqueryでローカルファイル(テキストファイル)の読込み</title>
- <script type="text/javascript" src="jquery-1.5.1.min.js"></script>
- <script type="text/javascript">
- function handleFileSelect() {
- if (window.File) {
- // File APIが実装のブラウザ
- var fileList = document.getElementById("files").files;
- var reader = new FileReader();
- reader.onload = function(){
- // ファイル読み込み完了時
- $("#list").html(reader.result);
- }
- // ファイルの読込み
- reader.readAsText(fileList[0], "utf-8");
- }
- else {
- // File APIが未実装のブラウザ
- $("#list").html("File APIが未実装のブラウザ");
- }
- }
- $(document).ready(function () {
- document.getElementById('files').addEventListener('change', handleFileSelect, false);
- });
- </script>
- </head>
- <body>
- <h1>jqueryでローカルファイル(テキストファイル)の読込み</h1>
- <div>
- <input type="file" id="files" name="files[]" multiple /><br />
- ▼読込んだファイル(テキストファイル)を表示する<br />
- <div style="border:1px solid #ccc;padding:0.5em;background:#f5f5f5;" id="list"></div>
- </div>
- <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
- <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
- <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
- </div>
- <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
- Copyright © 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
- </div>
- </body>
- </html>