Server.CreateObject(“Scripting.FileSystemObject”)问题 - 所需对象:服务器(代码:0)ASP / VBscript

时间:2014-03-13 08:19:22

标签: asp.net vbscript createobject

我想在我的服务器上创建一个文件,然后在

中写入数据
<script runat="server" language="VBScript">
        Function saveData() 
            Const ForReading = 1, ForWriting = 2 
            Dim fso, f 
            Set fso = Server.CreateObject("Scripting.FileSystemObject") 
            Set f = fso.OpenTextFile("ecr.txt", 8,true) 
            f.WriteLine("osgfouds") 
        End Function
</script>

我的浏览器出现错误,告诉我需要的对象:服务器&#39;在&#39; Server.CreateObject&#39;行

2 个答案:

答案 0 :(得分:0)

Server.createobject将用于服务器本身的VBScript / ASP脚本。由于这个原因,客户端浏览器将无法支持服务器。

作为补充说明。您需要关闭文件对象(f),因为它会保持文件打开并在您尝试再次写入时导致错误。另外,我添加了ForAppending位,以便您可以在fso.opentext文件中指定它。

所以要修复你的脚本:

<script runat="server" language="VBScript">
        Function saveData() 
            Const ForReading As String = 1
            Const ForWriting As String = 2
            Const ForAppending As String = 8

            Dim fso as Object
            Dim f as Object
            Set fso = CreateObject("Scripting.FileSystemObject") 
            Set f = fso.OpenTextFile("ecr.txt", ForAppending, true) 
            f.WriteLine("osgfouds") 
            f.Close
        End Function
</script>

修改

这是来自&gt;的更新问题Here

修改

好的,看看你之前的问题和这个问题。事情就是这样:ASP在服务器级运行并将vbscript加载到网站界面。直接附加到ASP的Vbscript将在服务器级别运行:

e.g。

<%
Const ForAppending = 8

dim fn,fp,fpn,wl,fulltext : fn = replace(formatdatetime(Now, 2), "/", "-") & ".txt"
Dim fso, msg :  fp = "C:\Users\...\Desktop\Logs\"
fpn = fp & fn
dim sep : sep = "==========================================================================="&vbcrlf
dim ssep : ssep = vbcrlf & "--------------------------------------"
fso = CreateObject("Scripting.FileSystemObject")

dim IPAddress, HostName, LUname
IPAddress = Request.ServerVariables("remote_addr")
If (fso.FileExists("C:\Users\...\Desktop\Logs\" & fn)) Then
    dim inSession,newuser
    wl = fso.OpenTextFile(fpn, ForAppending, True) 
    inSession = fso.OpenTextFile("C:\Users\...\Desktop\Logs\" & fn, 1)
    fulltext = inSession.ReadAll
'.....Code continues'
%>

因此,如果您尝试激活click事件并将其附加到VBScript以写入服务器端的文件,则无法正常工作,因为vbscript将尝试将其写入客户端。

为用户条目更新设计asp / vbscript的正确方法需要以下列方式执行:

浏览器 - 点击 - &gt;请求服务器 - &gt;服务器进程请求 - &gt;提供新页面 - &gt;浏览器

提供的证据 - &gt; Here

但是,您仍然可以使用XMLHTTPRequest或Ajax / Javascript来激活脚本。实际上,有趣的是,我刚刚问过how to execute a very basic script like this recently。所以这是如何做到的:

You have your HTML file(whatever.html):
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type ="text/javascript" >
    $(function () {
        $("#button").click(function () {
        $.get("test2.aspx", {
            loadData: "John"
        })
            .done(function (data) {
            if (data === "Fail") {
                alert("Logging Failed!");
            } else {
                alert("Logged Success!");
            }
        })
            .fail(function () {
            alert("error");
        });
    });
});
    </script>
</head><body>
<input type="button" id="button" value="Ecriture"></body>

你有ASPX文件(test2.aspx):

<%@ Page Language="VB" %>
<%
        Dim LogData As String : LogData = Request.Params("loadData")
        Dim SaveFile As String
        Const ForReading As Integer = 1
        Const StorageDirectory = "C:\Users\...\Desktop\Logs\serverlog.txt"
        Const ForWriting As Integer = 2
        Const ForAppending As Integer = 8
        If Len(LogData) = 0 Then LogData = "{EMPTY STRING}"
        Dim fso As Object
        Dim f As Object
        fso = CreateObject("Scripting.FileSystemObject")
        f = fso.OpenTextFile(StorageDirectory, ForAppending, True)
        f.WriteLine("New Entry:" & LogData)
        f.Close()
        If Err.Number <> 0 Then
            SaveFile = "Fail"
        Else
            SaveFile = "Success"
        End If
        Response.Write(SaveFile)
%>

注意 StorageDirectory必须是共享网络文件夹,以便服务器可以维护更新文件。

我已经测试了这段代码,但它确实有用。祝你好运

答案 1 :(得分:0)

这将在VB.NET中运行。试试这个

    Dim oFs
    Dim vSharePath
    Dim vFolder
    Dim vPath
    Dim objTStream
    vSharePath = ConfigurationManager.AppSettings("NetworkPath").ToString

    vFolder = Year(Date.Now) & Month(Date.Now).ToString & Date.Now.Hour.ToString & Date.Now.Second.ToString

    vPath = vSharePath & "\" & vFolder

    oFs = Server.CreateObject("Scripting.FileSystemObject")
    If Not (oFs.FolderExists(vPath)) Then
        Call oFs.CreateFolder(vPath)
        objTStream = oFs.CreateTextFile(vPath & "\test.txt", True)
        'Write some text to the file
        objTStream.WriteLine("Hello World!")
        objTStream.WriteLine()
        objTStream.WriteLine("This is my first text file!")
        'Close the TextStream object
        objTStream.Close()
        'Free up resources
        objTStream = Nothing
    End If
    oFs = Nothing

http://webcheatsheet.com/asp/filesystemobject_object.php