2012-12-04 17 views
31

में टेक्स्ट फ़ाइल को पढ़ने के लिए कैसे मैं अपनी जावास्क्रिप्ट फ़ाइल में एक टेक्स्ट फ़ाइल लोड करने की कोशिश कर रहा हूं और फिर जानकारी प्राप्त करने के लिए उस फ़ाइल से लाइनों को पढ़ना चाहता हूं, और मैंने फ़ाइल रीडर की कोशिश की लेकिन यह काम नहीं कर रहा है। क्या कोई मदद कर सकता है?जावास्क्रिप्ट

function analyze(){ 
    var f = new FileReader(); 

    f.onloadend = function(){ 
     console.log("success"); 
    } 
    f.readAsText("cities.txt"); 
} 
+3

पढ़ें: http://www.html5rocks.com/en/tutorials/file/dndfiles/। यदि यह एक स्थानीय फ़ाइल है, तो उपयोगकर्ता को सुरक्षा कारणों से फ़ाइल का चयन करना होगा। संबंधित: http://stackoverflow.com/questions/13428532/using-a-local-file-as-a-data-source-in-javascript – NullUserException

उत्तर

41

हाँ यह FileReader के साथ संभव है, मैं पहले से ही इस का एक उदाहरण किया है, यहाँ कोड है:

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Read File (via User Input selection)</title> 
    <script type="text/javascript"> 
    var reader; //GLOBAL File Reader object for demo purpose only 

    /** 
    * Check for the various File API support. 
    */ 
    function checkFileAPI() { 
     if (window.File && window.FileReader && window.FileList && window.Blob) { 
      reader = new FileReader(); 
      return true; 
     } else { 
      alert('The File APIs are not fully supported by your browser. Fallback required.'); 
      return false; 
     } 
    } 

    /** 
    * read text input 
    */ 
    function readText(filePath) { 
     var output = ""; //placeholder for text output 
     if(filePath.files && filePath.files[0]) {   
      reader.onload = function (e) { 
       output = e.target.result; 
       displayContents(output); 
      };//end onload() 
      reader.readAsText(filePath.files[0]); 
     }//end if html5 filelist support 
     else if(ActiveXObject && filePath) { //fallback to IE 6-8 support via ActiveX 
      try { 
       reader = new ActiveXObject("Scripting.FileSystemObject"); 
       var file = reader.OpenTextFile(filePath, 1); //ActiveX File Object 
       output = file.ReadAll(); //text contents of file 
       file.Close(); //close file "input stream" 
       displayContents(output); 
      } catch (e) { 
       if (e.number == -2146827859) { 
        alert('Unable to access local files due to browser security settings. ' + 
        'To overcome this, go to Tools->Internet Options->Security->Custom Level. ' + 
        'Find the setting for "Initialize and script ActiveX controls not marked as safe" and change it to "Enable" or "Prompt"'); 
       } 
      }  
     } 
     else { //this is where you could fallback to Java Applet, Flash or similar 
      return false; 
     }  
     return true; 
    } 

    /** 
    * display content using a basic HTML replacement 
    */ 
    function displayContents(txt) { 
     var el = document.getElementById('main'); 
     el.innerHTML = txt; //display output in DOM 
    } 
</script> 
</head> 
<body onload="checkFileAPI();"> 
    <div id="container">  
     <input type="file" onchange='readText(this)' /> 
     <br/> 
     <hr/> 
     <h3>Contents of the Text file:</h3> 
     <div id="main"> 
      ... 
     </div> 
    </div> 
</body> 
</html> 

यह भी एक ही बात IE के कुछ पुराने संस्करण का समर्थन करने के लिए क्या किया जा सकता है (मुझे लगता है कि 6 -8) ActiveX ऑब्जेक्ट का उपयोग करके, मेरे पास कुछ पुराना कोड था जो कि कुछ समय हो गया है, इसलिए मुझे इसे खोदना होगा। मुझे एक समाधान मिला है जिसकी मैंने Jacky Cui's blog की सौजन्य का उपयोग किया और संपादित किया यह उत्तर (थोड़ा सा कोड साफ़ भी)। आशा करता हूँ की ये काम करेगा।

आखिरकार, मैंने कुछ अन्य उत्तरों को पढ़ा जो मुझे आकर्षित करने के लिए हराते हैं, लेकिन जैसा कि वे सुझाव देते हैं, आप शायद ऐसे कोड की तलाश कर रहे हैं जो आपको सर्वर (या डिवाइस) से टेक्स्ट फ़ाइल लोड करने देता है जहां जावास्क्रिप्ट फ़ाइल बैठी है । अगर ऐसी बात है तो है आप दस्तावेज़ गतिशील लोड करने के लिए जो कुछ किया जाएगा इस प्रकार है AJAX कोड हैं:

<!DOCTYPE html> 
<html> 
<head><meta charset="utf-8" /> 
<title>Read File (via AJAX)</title> 
<script type="text/javascript"> 
var reader = new XMLHttpRequest() || new ActiveXObject('MSXML2.XMLHTTP'); 

function loadFile() { 
    reader.open('get', 'test.txt', true); 
    reader.onreadystatechange = displayContents; 
    reader.send(null); 
} 

function displayContents() { 
    if(reader.readyState==4) { 
     var el = document.getElementById('main'); 
     el.innerHTML = reader.responseText; 
    } 
} 

</script> 
</head> 
<body> 
<div id="container"> 
    <input type="button" value="test.txt" onclick="loadFile()" /> 
    <div id="main"> 
    </div> 
</div> 
</body> 
</html> 
+0

पोस्ट के लिए धन्यवाद! हालांकि, ऐसा कुछ है जो मुझे समझ में नहीं आता है: 'e.target' के बजाय 'पाठक' या 'यह' क्यों उपयोग नहीं किया जाता है, जबकि वे सभी 'फ़ाइल रीडर' ऑब्जेक्ट को संदर्भित करते हैं: ** [डेमो] (http: // jsfiddle.net/Mori/tJBHZ/)**। – Mori

+0

"यह" कीवर्ड के लिए, वास्तव में केवल एक व्यक्तिगत प्राथमिकता बात है, जब तक कि किसी तत्व पर इसकी इनलाइन न हो, मैं इसके साथ परेशान नहीं हूं ... http://tech.pro/tutorial/1192/avoiding-the-this-problem -इन-जावास्क्रिप्ट "पाठक" के लिए, हाँ, यह वैध बिंदु है, लेकिन फिर, किसी आइटम को ऐसे तरीके से उपयोग नहीं करना पसंद करता है जो भ्रमित रूप से "पढ़ता है"। यदि किसी ऑब्जेक्ट को संदर्भित करने के कई तरीके हैं, तो मैं कहूंगा कि बाद में पढ़ने के साथ आप सबसे अधिक आरामदायक हैं। – bcmoney

9

जावास्क्रिप्ट को सुरक्षा कारणों से उपयोगकर्ता के फाइल सिस्टम तक पहुंच नहीं है। FileReader केवल उपयोगकर्ता द्वारा मैन्युअल रूप से चुनी गई फ़ाइलों के लिए है।

+2

यह मान रहा है कि ओपी क्लाइंट कंप्यूटर पर एक फ़ाइल के बारे में बात कर रहा है। अगर यह सर्वर पर कुछ उपलब्ध है तो इसे AJAX के माध्यम से लोड किया जा सकता है। –

10

यह काफी आसानी से जावास्क्रिप्ट XMLHttpRequest() वर्ग (AJAX) का उपयोग किया जा सकता है:

function FileHelper() 

{ 
    FileHelper.readStringFromFileAtPath = function(pathOfFileToReadFrom) 
    { 
     var request = new XMLHttpRequest(); 
     request.open("GET", pathOfFileToReadFrom, false); 
     request.send(null); 
     var returnValue = request.responseText; 

     return returnValue; 
    } 
} 

... 

var text = FileHelper.readStringFromFileAtPath ("mytext.txt"); 
+1

मेरे लिए काम नहीं किया – Daniel

2

मेरा उदाहरण

<html> 
    <head> 
     <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css"> 
     <script src="http://code.jquery.com/jquery-1.10.2.js"></script> 
     <script src="http://code.jquery.com/ui/1.11.3/jquery-ui.js"></script> 
    </head> 
    <body> 
     <script> 
      function PreviewText() { 
      var oFReader = new FileReader(); 
      oFReader.readAsDataURL(document.getElementById("uploadText").files[0]); 
      oFReader.onload = function (oFREvent) { 
       document.getElementById("uploadTextValue").value = oFREvent.target.result; 
       document.getElementById("obj").data = oFREvent.target.result; 
      }; 
     }; 
     jQuery(document).ready(function(){ 
      $('#viewSource').click(function() 
      { 
       var text = $('#uploadTextValue').val(); 
       alert(text); 
       //here ajax 
      }); 
     }); 
     </script> 
     <object width="100%" height="400" data="" id="obj"></object> 
     <div> 
      <input type="hidden" id="uploadTextValue" name="uploadTextValue" value="" /> 
      <input id="uploadText" style="width:120px" type="file" size="10" onchange="PreviewText();" /> 
     </div> 
     <a href="#" id="viewSource">Source file</a> 
    </body> 
</html> 

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^