0

I am dynamically generating an HTML file for the print option.

After the print window has been opened and the file printed, I want to remove the HTML file from the folder.

I need to do this with JavaScript. For this I am using the following code:

var myObject = new ActiveXObject("Scripting.FileSystemObject");
var myFolder = myObject.GetFile(strReportFilePath);
myFolder.Delete();`

But this only works in IE but not in Firefox, so how do I delete the file with a JavaScript function?

Alex Nolasco
  • 18,750
  • 9
  • 86
  • 81
MNR
  • 1,073
  • 7
  • 23
  • 37
  • On the client or on the server? What browsers do you need to support? How are you generating the HTML file? – Pekka Aug 18 '11 at 12:54
  • I didn't know you can actually do it in IE, I thought it was impossible. I think it is impossible in Firefox, though. – Igor Zinov'yev Aug 18 '11 at 12:55
  • 2
    The simple answer is "you cannot". Security model does not allow javascript to interact with the client file system. If it were possible, then, theoretically, any page from any website could delete any file from your machine. – Aleks G Aug 18 '11 at 12:56
  • IE and mozilla i need to support.. – MNR Aug 18 '11 at 12:56
  • ok.... Thanq for all ur quick replay – MNR Aug 18 '11 at 12:57
  • 1
    You used Jscript, not Javascript. JScript is a "Javascriptextension" as the a part of Windows Scripting Host (short WSH). Becaused it uses the DCOM, you can only execute it with browsers whose can deal with ActiveX components. The most famous representative of those is the Internet Explorer. – Reporter Aug 18 '11 at 13:02
  • 1
    I repeat. Where is the HTML file located, on the client or on the server? That is the main question here. – Pekka Aug 18 '11 at 13:02
  • The `new ActiveXObject` should be enough to tell you this is not a cross-browser/"javascript" question. – Brian Aug 18 '11 at 13:10

2 Answers2

5

You can't. JavaScript is sandboxed. With IE, you are using ActiveX to do the dirty work.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • My guess is he is trying to delete files on server side, and the JScript snippet was just the first one to show up in a search – Pekka Aug 18 '11 at 13:04
1

I'm on the look out for just accessing files on the local files and failed to find a way that works cross-browser easily yet. However you might want to try signed Java applets which seemed to be a (though not so smooth) solution.

You also might want to keep track of this

Community
  • 1
  • 1
Diff.Thinkr
  • 855
  • 1
  • 8
  • 16