2016-03-01 63 views
1

我使用的是经典的ASP,并试图将特定文本文件的内容打印到屏幕上。我知道如何通过ASP在VBScript中做到这一点,但是如何通过ASP在JavaScript中做到这一点?从使用Javascript的经典ASP文件中读取文本

+0

可能有帮助,如果逃跑在Windows文件路径中的反斜杠你可以发布你的工作VBS例子 – John

+1

你的意思是发球R方JS还是客户端?那些是完全不同的东西。 –

+0

服务器端JS。 –

回答

1

如果你使用普通的JavaScript,你可以做如下的事情。请记住,将get函数的第一个参数替换为实际的文件路径,包括文件扩展名(例如:myfilename.txt)。您还必须确保您尝试打开的文件来自同一个域。这里是一个如何工作的例子的链接(http://bytewarestudios.com/launchjs/get)。我从我写的JavaScript库中删除了get函数,因此您不必加载整个库。

HTML:

<div id="results"></div> 

的JavaScript(将此代码的脚本标签的结束标记前右):

//call the get function 
    get(pathToTextFile,function(data){ 

     //display the file contents to the screen. 
     document.getElementById("results").innerHTML = data; 


    }); 



function get(url,fn){//begin ajax function 

    var contentType; 

    //variable to hold the xmlhttp object 
    var xmlhttp = null; 

    //if a contentType is not passed in 
    if(typeof arguments[1] === "function"){//begin if then else 

     //set it to default of text/html 
     contentType = "text/html"; 

    }//end if then 
    else{ 

     //set the contentType to the argument passed in 
     contentType = arguments[1]; 

    }//end if then else 



    //if the browser contains the object 
    if(window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari 

      //create a new XMLHttpRequest object 
      xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 

      //create a new ActiveXObject 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 

    }//end if then else 

     //add the event listenter 
     xmlhttp.onreadystatechange = function(){  

    //if the status of the request is good 
    if (xmlhttp.readyState===4 && xmlhttp.status===200){//begin if then 

    //get the response text and store it in the data variable 
    var data = xmlhttp.responseText;  

    //call the ajaxDone callback function 
    ajaxDone(data,fn); 

    }//end if then 



};//end function 

    function ajaxDone(data,fn){//begin function 

    //call the anonymous function passing the data returned from the xmlhttp request 
    fn(data);  



}//end function 
+1

我认为OP意味着使用服务器端JS而不是VBS。 – John

+0

@约翰这是一个50/50的机会,这个问题本身并不明确。 –

+0

@ShadowWizard - 我认为他谈论“通过ASP的VBS”的方式,然后“JS通过ASP”意味着它是服务器端,但我同意这是模糊的。 – John

1

它基本上只是翻译您的VBS成JS的情况下,它的没有那么难,只要你对这两方面有基本的了解。

VBScript示例

<%@ LANGUAGE="VBSCRIPT" %> 
<% 
dim fso, txt, content 
set fso = Server.CreateObject("Scripting.FilesystemObject") 
set txt = fso.OpenTextFile("C:\Pathto\textfile.txt") 
content = txt.ReadAll 
Response.Write content 
%> 

JScript的例子

<%@ LANGUAGE="JSCRIPT" %> 
<%  
var fso = Server.CreateObject("Scripting.FileSystemObject"); 
var txt = fso.OpenTextFile("C:\\Pathto\\textfile.txt"); 
var content = txt.ReadAll(); 
Response.Write(content); 
%> 

请注意,你需要,如果你正在使用JS