Can't write file in classic asp

2019-07-06 10:58发布

Ok, it's been a while since I've worked with classic asp so I'm a bit rusty. Here's my question.

I'm trying to write a file to the file system using FSO. The code below is very simple. However, the file is not appearing and no errors are appearing. I know it's running the code because I can add response.writes before and after this snippet and they both appear in the output. However, no file is created, no error is thrown. I've even changed it so it's a bogus path to force an error. No dice. I added everyone to have read and write on the directory permissions. Still the same.

Ideas?

Here's my code:

Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

'Open the text file
Dim objTextStream
Set objTextStream = objFSO.OpenTextFile("d:\test.txt", True)

'Display the contents of the text file
objTextStream.WriteLine "howdy"

'Close the file and clean up
objTextStream.Close
Set objTextStream = Nothing
Set objFSO = Nothing

2条回答
姐就是有狂的资本
2楼-- · 2019-07-06 11:08

Try this:

<%

if Append = true then
   iMode = 8
else 
   iMode = 2
end if
set oFs = server.createobject("Scripting.FileSystemObject")
set oTextFile = oFs.OpenTextFile("C:\wwwroot\Test.txt", iMode, True)
oTextFile.Write "Test Content"
oTextFile.Close
set oTextFile = nothing
set oFS = nothing

%>

I tried this locally, after setting the permissions on my directory and it worked.

You can get the original source from here: http://www.freevbcode.com/ShowCode.Asp?ID=89

查看更多
ゆ 、 Hurt°
3楼-- · 2019-07-06 11:17

The only possible reason that the code would not produce an error message, is that you have this in your page:

On Error Resume Next

That's bad, for the reason that you have just seen. It just silently ignores any error messages, and leaves you without a clue to why it's not working as expected.

(It should only be used for isolated parts of the code where you anticipate an error, and actually check for an error condition after each operation.)

Remove that from your page, and the error message that you probably get is that the parameters are invalid for the call on this line:

Set objTextStream = objFSO.OpenTextFile("d:\test.txt", True)

You have forgotten the second parameter, which is the I/O mode. You should use the value 1 for writing:

Set objTextStream = objFSO.OpenTextFile("d:\test.txt", 1, True)

Alternatively, you can use the CreateTextFile method instead:

Set objTextStream = objFSO.CreateTextFile("d:\test.txt", True)
查看更多
登录 后发表回答