Script w/ FileSystemObject, error creating object

G

Guest

I keep seeing that you can use the FileSystemObject in either VB script, or
Javascript on an aspx page.

I added a refrence to the scrrun.dll
I added importing namespaces for 'System.Object', 'Scripting',
'Scripting.FileSystemObject', and a few others

However, when I try to create the fso object I keep receiving an error.

'ActiveX component can't create object: 'Scripting.FileSystemObject'

What am I missing?
All I am trying to do is delete a file on the onbeforeunload event.
I have spent the better part of two days on this.
All help greatly appreciated.
 
K

Ken Cox [Microsoft MVP]

Are you trying to delete a file on the user's system? If so, the browser's
security is going to make this difficult if not impossible.

Apart from that, it looks like you are trying to mix .NET server-side code
with client-side Javascript in a way that can't be done.
 
G

Guest

I am trying to delete the file off of the server or another known
machine/path on the network. I am using a control that requires an XML file
to load. I don't want people stepping on each other so I generate a file
name with a random number and want to delete the file when the aspx page
unloads.
I cannot use the vb unload event, it occurs if the page is refreshed.
Possibly I could use the 'Disposed' event, but not sure how to trigger it.

I concur about mixing server/client code but I keep seeing threads that
indicate you can use the FileSystemObject in client script (javascript or
vbscript).

Frustrating.
 
K

Kevin Spencer

Hi kermit,

Remove the reference to scrrun.dll. Remove the import statements. Remove all
code that attempts to use VBScript. And use the classes of the CLR to do
your work, specifically, the System.IO namespace for managing file and
directories.

ASP.Net has very little in common with ASP, other than the fact that they
both do similar types of work in the same environment. ASP.Net is Managed
code. VBScript is not. ASP.Net is object-oriented. ASP is procedural. And
they are NOT compatible. You can use Interop to emulate compatibility, but
only as a last resort. It is NOT a good idea as a rule, and can
unnecessarily complicate your app and make it much harder for you to manage.

If you're uncomfortable with the ASP.Net programming paradigm, you might
want to stick with ASP for now.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
K

Ken Cox [Microsoft MVP]

For deleting a file on the server, you should use one of the .NET classes
(File Class) rather than the FileSystemObject which is COM technology:

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemiofileclassdeletetopic.asp?frame=true

You'll have a problem tracking the browser's unload event because that's a
client-side event. If the person types in a new URL, you'll be in trouble.

You might be better off to use the server-side Session_End event in the
global.asax file.

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session ends
End Sub

However, that would leave the file hanging around for the length of the
Session timeout which is 20 minutes by default. It would be faster if you
could get people to click a logout button on which you would call
Session.Abandon.


Ken
Microsoft MVP [ASP.NET]
 
G

Guest

I need to get comfortable with it. 90% of the time, plus, I'm OK. Then I
get tangled up in something like this.

OK, I got rid of scrrun.dll and am back to where I started, trying to use
the System.IO File.Delete(sfilename).

If I put the following in the <BODY> tag, it works, but at the wrong time
and only once.
<BODY onbeforeunload="<% File.Delete(hdnXMLfile.value) %>" ......

I tried putting an "if hdnUnload.value=True then .....

Again, since Server code only runs at render time on an aspx page, that did
not work at page unload.

How would you do this? I would really like to not leave the files until
Session_End.
 
K

Kevin Spencer

Hi kermit,

Well, you're still trying to do OOP in a procedural fashion, and that's
going to continue to bite you as you go.

First, the <body> tag has nothing to do with the server-side programming. It
is there as part of the client-side HTML document. The "onbeforeunload"
event is, again, a client-side event, and doesn't happen or even have
anything to do with the server.

I'm going to do a little guesswork here, and guess that you have a file on
the server that was created sometime during the lifetime of the page in
question. Apparently, you want to delete the file when the doument is
navigated away from, the user Session ends, or the browser is closed. In any
case, the best bet is (probably) to use the Session_End server-side event to
do your file cleanup. IOW, this would not be any part of the page's
server-side code, but would be part of the Session_End event handler in your
global.asax class.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
G

Guest

Yes, I realize that all HTML tags are client side. I would assume for
correct object orented coding the web page should be 'controlled' by the
server. But it seems that there is often a need to do some clean up
when a browser window is closed (not the session or app, just a window
used by the application) and since it is not possible to ensure the user
always uses a 'Close' button, I am confused as to how to notify, or how
the server is to know, that the window has been closed - do cleanup.
I have given up and did use the session_end event. But it does not
seem to always trigger, esp. if the user closes the app with other than
the logout button, i.e. maybe browses to an entirely different place.
Your assumptions on what I am trying to do is correct. When the page
opens I create a file on the server (XML) and want to remove it when I am
done with it.
It has to last long enough for the page to complete loading all
controls.
I write the file to a dir on the server (or other network machine,
this is an intranet app). I cannot use a data island to pop. the control.
I guess I will write a app to run on a timer and delete files older than
x on regular basis.
XML is so heavly used, shopping carts, etc. I assume many (most) are
creating files, not data islands, what do people do to remove xml files no
longer needed?
Thank you for your assistance. My issue is complete. But if you have
any comments to add I would love to hear them.
 
G

Guest

For any interested, I found a better work-a-round.
I used the client <BODY onunload...
to set the page location to a different aspx page (cleanup.aspx)
I used the server side on_load event to clean up (delete) my unwanted files.
Works rather or not the user clicks the 'Close window button' or just closes
the window.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top