2016-03-01 191 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