peterS.: Datei laden

Beitrag lesen

gruss martin,

Hallo, kann man mit javascript eine Textdatei in ein array oder in einen string laden?

unter windows/gecko,msie,opera7x kannst Du txt-files
in fenster oder frames laden, deren inhalt wiederum
einem string zugewiesen werden kann -  das folgende
beispiel ist kommentiert - erfolg fuer alle browser
ist Dir aber nur beschieden,  wenn  Du tatsaechlich
vorhandene txt-files (msie schafft es naemlich auch
mit document.open("text/plain") und einem "virtuellen"
textstream) verwendest  -  wie diese datei fuer das
beispiel beschaffen sein soll, ist ebenfalls im
kommentar nachzulesen  -  so, hier nun der code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<head>
 <meta http-equiv="content-style-type" content="text/css" />
 <meta http-equiv="content-script-type" content="text/javascript" />
 <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
 <title>tryToReadText.html</title>
 <script type="text/javascript" language="JavaScript">
 <!--
  var textFileWindow = new Object();textFileWindow.closed = true;
  var textFileString = "";
 // var textFileString = ""+           // zur pruefung des beispiels:
 //  "row00: dummyText dummyText dummyText dummyText dummyText\n"+ //
 //  "row01: dummyText dummyText dummyText dummyText dummyText\n"+ // den inhalt dieser variable
 //  "row02: dummyText dummyText dummyText dummyText dummyText\n"+ // bitte als separaten textfile
 //  "row03: dummyText dummyText dummyText dummyText dummyText\n"+ // mit namen "tryToReadText.txt"
 //  "row04: dummyText dummyText dummyText dummyText dummyText\n"+ // umsetzen und zusammen mit dem
 //  "row05: dummyText dummyText dummyText dummyText dummyText\n"+ // beispiel "tryToReadText.html"
 //  "row06: dummyText dummyText dummyText dummyText dummyText\n"+ // in ein verzeichnis legen;
 //  "row07: dummyText dummyText dummyText dummyText dummyText\n"+ //
 //  "row08: dummyText dummyText dummyText dummyText dummyText\n"+ // funktioniert unter windows mit
 //  "row09: dummyText dummyText dummyText dummyText dummyText\n"; // gecko-engines, msie sowie opera 7.x
  function opentextFileWindow(content) {
   if (textFileWindow.closed) {
   // textFileWindow = window.open("","","width=500,height=600,scrollbars");
    textFileWindow = window.open("tryToReadText.txt","textWin","width=500,height=600,scrollbars");
   }
  // textFileWindow.document.open("text/plain","replace");
  // textFileWindow.document.write(content);
  // textFileWindow.document.close();
   if (location.replace || document.all) {
    textFileWindow.location.replace("tryToReadText.txt");
   }
   textFileWindow.focus();
  }
  function getWindowContent() {
   if (textFileWindow && !textFileWindow.closed) {
    if (textFileWindow.document && textFileWindow.document.documentElement) {
     var textObject = textFileWindow.document.documentElement;
    // alert(typeof textObject);
     var property = "";
     var objectLog  = "";
     for (property in textObject) {
      objectLog += property + "\t: " + textObject[property] + "\n";
     }
    // alert(objectLog);
    // alert(textObject.innerHTML);
     var windowFileText = textObject.innerHTML;

// opentextFileWindow(objectLog);

if (!textFileWindow.closed) {
      textFileWindow.document.open("text/plain","replace");
      textFileWindow.document.write(objectLog);
      textFileWindow.document.close();
     }

// inhalt eines *.txt-files unter WIN/gecko,msie,opera7x bestimmen !!
    /*
     so wie es aussieht, bilden alle oben genannten browser
     die inhalte von files mit dem mime "text/plain" innerhalb
     eines eigenen html-containers als <pre>-formatierten text ab;

auf inhalte von textfiles kann in diesen browsern demzufolge
     ueber [frame/window-object].document.documentElement.innerHTML
     zugegriffen werden  -  der browsereigene html-code muss dann
     nur noch mit entsprechenden string.methoden entfernt werden;
    */
     windowFileText = windowFileText.substring(windowFileText.toLowerCase().indexOf("<body><pre>")+11);
     windowFileText = windowFileText.substring(0,(windowFileText.toLowerCase().lastIndexOf("</pre></body>")));
     alert(windowFileText);
     if (!textFileWindow.closed) {
      textFileWindow.focus();
     }
    }
   }
  }
  function startApplication() {
   opentextFileWindow(textFileString);
   setTimeout("getWindowContent()",3500);
  }
 //-->
 </script>
 <style type="text/css">
 <!--
 body {
  margin-left:0px;
  margin-top:0px;
  margin-right:0px;
  margin-bottom:0px;
  background-color:#efefef;
  background-image:none;
  font-family:verdana,geneva,helvetica,arial,sans-serif;
  font-weight:normal;
  color:#000000;
  font-size:12px;
  line-height:14px;
 }
 //-->
 </style>
</head>

<body onload="startApplication()" bgcolor="#efefef" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
</body>

</html>

viel erfolg - by(t)e by(t)e - peterS. - pseliger@gmx.net