2014-03-13 26 views
1

我要創建我的服務器上的文件,然後在寫DATAS的Server.CreateObject(「Scripting.FileSystemObject的」)的問題 - 所需的對象:服務器(代碼:0)ASP/VBscript的

<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> 

在我的瀏覽器中出現錯誤,告訴我在'Server.CreateObject'行有'object required:server'對象

回答

0

Server.createobject將用於服務器本身的VBScript/ASP腳本。出於這個原因,客戶端瀏覽器將無法支持服務器。

作爲補充說明。你需要關閉你的文件對象(f),因爲它會保持文件打開,並在你嘗試再次寫入文件時導致錯誤。另外,我添加了ForAppending位,以便您可以在您的fso.opentextfile中指定它。

因此,要解決您的腳本:

<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> 

編輯

這是從更新的問題 - >Here

編輯

好吧,看你以前的問題和這一個。這是事情:ASP運行在服務器級別,並加載到網站界面的VBScript。直接附加到ASP的Vbscript將在服務器級別運行:

例如,

<% 
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來設計爲用戶條目更新的正確方法需要在以下方式執行:

瀏覽器 - 點擊 - >請求給服務器 - >服務器進程請求 - >提供新的一頁 - >瀏覽器

提供的證據 - >Here

但是,你仍然可以使用一個XMLHttpRequest或阿賈克斯/ 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必須是一個共享的網絡文件夾,以便服務器可以保持更新文件。

我測試過這段代碼,它能正常工作。好運

+0

我已經嘗試過這樣做,但是,它創建於客戶端的文件...(在用戶的桌面上是精確的) – Geo

+0

你可以發佈你的整個代碼然後。看起來這個問題可能與你如何執行代碼有關。 – Rich

+0

看看我上面更新的代碼。應該解決你的問題。 – Rich

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