ASP从文本框到文本文件的写入值

时间:2015-03-28 20:54:08

标签: vb.net asp-classic textarea writetofile

我有一个textarea(id = output2 ),其中包含一个数字列表。我有ASP代码将数据写入文本文件,如果我指定文本,它就可以工作。

写入文本文件的代码:

<%
function WriteToFile(FileName, Contents, Append)
on error resume next

if Append = true then
   iMode = 8
else 
   iMode = 2
end if
set oFs = server.createobject("Scripting.FileSystemObject")
set oTextFile = oFs.OpenTextFile(FileName, iMode, True)
oTextFile.Write Contents
oTextFile.Close
set oTextFile = nothing
set oFS = nothing

end function

%>

然后,此代码将覆盖文件并插入数据AND WORKS:

<%
WriteToFile "C:\INSTALL\Test1.txt", "Why is this so difficult??", False
%>

但是,如果我单独留下第一个代码并尝试从textarea字段获取动态数据,它就会失败并且没有错误。甚至不触摸文本文件。我尝试了很多方法......这里有五个失败:

1:

<%
WriteToFile "C:\INSTALL\Test1.txt", Document.getElementById("output2"), False
%>

2:

<%
dim texttoinsert
texttoinsert = Document.getElementById("output2")
WriteToFile "C:\INSTALL\Test1.txt", texttoinsert, False
%>

3:

<%
WriteToFile "C:\INSTALL\Test1.txt", Response.Write(Document.getElementById("output2")), False
%>

4:

<%
dim texttoinsert
texttoinsert = "Starting to hate this"
WriteToFile "C:\INSTALL\Test1.txt", texttoinsert, False
%>

5:

<%
dim texttoinsert
texttoinsert = "Definitely hate this"
WriteToFile "C:\INSTALL\Test1.txt", Response.Write(texttoinsert), False
%>

我甚至做了一些能够利用Document.getElementById的VBscript,但我无法弄清楚如何让它进入ASP代码。

2 个答案:

答案 0 :(得分:2)

document.getElementByID在服务器端代码中没有任何意义。您必须提交表单以将数据传递到功能页面,然后像这样使用它:

html页面

<form action="myfunction.asp">
<input name="texttoinsert">
<input type="submit" value="write data">
</form>

<强> myfunction.asp

<%
function WriteToFile(FileName, Contents, Append)
 on error resume next
 if Append = "true" then
    iMode = 8
 else 
    iMode = 2
 end if
 set oFs = server.createobject("Scripting.FileSystemObject")
 set oTextFile = oFs.OpenTextFile(FileName, iMode, True)
 oTextFile.Write Contents
 oTextFile.Close
 set oTextFile = nothing
 set oFS = nothing
end function

texttoinsert = request("texttoinsert")
WriteToFile "C:\INSTALL\Test1.txt", texttoinsert, False
%>

答案 1 :(得分:0)

将变量传递给函数时,不需要Response.write。

<%dim texttoinsert
texttoinsert = "Definitely hate this"
WriteToFile "C:\INSTALL\Test1.txt", texttoinsert, False
%>