【问题标题】:Read text from file in classic ASP using Javascript使用 Javascript 从经典 ASP 文件中读取文本
【发布时间】:2016-03-01 02:00:50
【问题描述】:

我正在使用经典的 ASP,并试图将特定文本文件的内容打印到屏幕上。我知道如何通过 ASP 在 VBScript 中执行此操作,但是如何通过 ASP 在 Javascript 中执行此操作?

【问题讨论】:

  • 如果您可以发布您的工作 VBS 示例可能会有所帮助
  • 你的意思是服务器端JS,还是客户端?这些是完全不同的东西。

标签: javascript iis asp-classic


【解决方案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

【讨论】:

  • 我认为OP意味着使用服务器端JS而不是VBS。
  • @John 这是一个 50/50 的机会,从问题本身来看真的不清楚。
  • @ShadowWizard - 我认为他谈论“通过 ASP 的 VBS”的方式,然后是“通过 ASP 的 JS”暗示它是服务器端,但我同意它是模棱两可的。
  • @John 好吧,直接问OP,看看能不能得到答案。
  • 我只有一个客户端解决方案。 @John 你可能是对的,但谁知道像你和影子巫师指出的那样。
【解决方案2】:

基本上就是把你的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,则需要转义 Windows 文件路径中的反斜杠

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    相关资源
    最近更新 更多