2013-09-25 1 views
0

내가 로컬 파일 및 디스플레이를 읽을 다음 한파일을 로컬에서로드하고 아약스없이 html javascript를 사용하여 내용을 표시하는 방법은 무엇입니까?

 <html> 
     <head> 
     <script> 
      function readfile() { 
     document.getElementById('iframe').contentDocument.body.firstChild.innerHTML; 
    } 
    </script> 
     </head> 
     <body> 
    <iframe id='iframe' src='txt2.txt' onload='readfile()'> </iframe> 
    <input type="file" id="fileinput" /> 
    </body> 
    </html> 

그러나 여기에서 당신은 이미 'txt2.txt'와 같은 파일 경로를 준,하지만 난 파일을주고 싶지 않아 볼 수 있습니다 이름을 미리, 대신 파일을로드하고 입력 내용을 사용하여 내용을 표시 할 = "파일", 어떻게 아약스를 사용하지 않고 이것을할까요?

+1

나는 당신이 로컬 파일을 읽을 수 있다고 생각하지 않습니다. 정말로, 당신이 할 수있는 것은 웹 서버에 POST 파일입니다. 어쩌면 HTML5, Flash 또는 Silverlight 구성 요소를 사용할 수 있습니까? –

+0

콘텐츠를 웹 페이지 자체의 일부로 푸시 (예 : 모든 요소 –

+0

) 할 수 있습니다. [Javascript, 로컬 파일을 읽는 방법?] (http://stackoverflow.com/questions/6506518/javascript-how- to-read-local-file) – RobAtStackOverflow

답변

0

브라우저에서 로컬 파일 읽기는 허용되지 않습니다. 이는 웹 사이트에서 파일을 읽고 정보를 도용하지 못하도록하기위한 것입니다.

자세한 내용은 두 번째 포스트 (답) 참조하십시오

대부분의 브라우저는 못하게 보안의 주요 위반 될 브라우저에서 로컬 파일을 읽을 수 Javascript, how to read local file?

을 페이지가 로컬 파일 시스템에서 비롯된 경우에도 로컬 파일을 읽습니다.

그러나 HTML5 file API을 사용해 볼 수 있습니다.

4

아래 코드는 파일을 검색하고 텍스트 영역에 콘텐츠를 복사합니다

<input type="file" id="fileinput" /> 
    <script type="text/javascript"> 
     function readSingleFile(evt) { 
     //Retrieve the first (and only!) File from the FileList object 
     var f = evt.target.files[0]; 

     if (f) { 
      var r = new FileReader(); 
      r.onload = function(e) { 
       var contents = e.target.result; 
      alert("Got the file.\n" 
        +"name: " + f.name + "\n" 
        +"type: " + f.type + "\n" 
        +"size: " + f.size + " bytes\n" 
        + "starts with: " + contents.substr(1, contents.indexOf("\n")) 
      ); 
      document.getElementById('area').value= contents; 
      } 
      r.readAsText(f); 

     } else { 
      alert("Failed to load file"); 
     } 
     } 

     document.getElementById('fileinput').addEventListener('change', readSingleFile, false); 
    </script> 

    <textarea rows=20 id="area"></textarea> 
관련 문제