La Web del Programador: Comunidad de Programadores
 
    Pregunta:  6119 - COMO ACCEDER A DATOS RECIBIDOS EN UN FICHERO DE TIPO TEXTO
Autor:  MªAngeles Gómez
Me gustaría saber como acceder a datos recibidos en un fichero de tipo texto, el cual reside en el servidor. Necesito leer y sacar en el navegador (preferiblemente IE) esos datos.

Gracias.

  Respuesta:  Javier Ramirez
Hola Mª Angeles,
Creo poder contestar tu pregunta. Lo que tienes que hacer es crearte un objeto ActiveX que sea del tipo Scripting.FileSystemObject para seguidamente recorrerlo.De tal manera un posible codigo seria:

function abrirFichero()
{
var fso,ageneral,contenido,root;
root="\\servidor\carpeta\fichero.txt"; //aki vendria la ruta entera donde esta alojado el fichero. Ejemplo:\\20.20.20.20\tmp\fichero.txt

fso = new ActiveXObject("Scripting.FileSystemObject");
if (fso.FileExists(root)) //si existe
{
ageneral = fso.OpenTextFile(root,1, true); //se abre el archivo
while(!ageneral.AtEndOfStream ) //lee hasta final de fichero
{
contenido=contenido+ageneral.ReadLine(); //lee linea y anyade a la variable
}
ageneral.Close(); //cierra fichero
alert(contenido);
}
else //si no existe
{
alert("El fichero no existe");
}
}

Espero que te sirva.
Saludos