Javascript: Save file with ActiveXobject - javascript

I am working on an Asp .net project and i have a textarea in an aspx page, and i am trying to save textarea contents on a file on the server by clicking a button with the following code:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c:\\temp1\\testfile.txt", true);
a.WriteLine(saveData);
a.Close();
The issue is if the file does not exists , then it creates it. But if it exists it dodes not overwrite it. Any help pls ? (I have to mention that Localy runing the application with visual studio then it rewrites it, but it doesnt works on the published version )

Use rather OpenTextFile() than CreateTextFile(). It also creates non-existing file if needed.
var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.OpenTextFile("c:\\temp1\\testfile.txt",2, true);
a.WriteLine(saveData);
a.Close();

Related

javascript to run client's oulook using ActiveXObject with attachment from the Downloads folder on the local system

I need help with this problem.
I use the code below to run the client's outlook new email with attachment,
it is was working perfectly when I was using the exact path of the file like
add. ("C:/USERS/MY USER NAME/Downloads/".fileName).
But when I changed it to Add("%userprofile%/Downloads/"+fileName) to access the current user logged in,
it didn't work with no errors, it seems can't find the file.
Is there something wrong?? Or different way to do that??
setTimeout(function(){
var theApp = new ActiveXObject("Outlook.Application");
var objNS = theApp.GetNameSpace('MAPI');
var theMailItem = theApp.CreateItem(0);
theMailItem.Attachments.Add("%userprofile%/Downloads/"+FileName);
theMailItem.display();
}, 2000);
The source of the attachment can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment. The method doesn't accept environment variables, you must specify the fully-qualified file path.
To get a fully-qualified file path you can use the following code:
var ObjShell = new ActiveXObject("Shell.Application");
var wShell = new ActiveXObject("WScript.Shell");
var path = wShell.ExpandEnvironmentStrings("%userprofile%\\Downloads\\");
So then you will be able to attach a file:
theMailItem.Attachments.Add(path+FileName);

Can I create a folder using Javascript program

Can I create a folder using Java script program like
var fso, fldr;
fso = new ActiveXObject("Scripting.FileSystemObject");
fldr = fso.CreateFolder("C:\\MyTest");
ActiveXObject is only available within Internet Explorer, also the correct permissions must be set before this is possible.
More info:
http://www.ezineasp.net/post/Javascript-FSO-CreateFolder-Method.aspx
Yes you can create, however it will only run IE browser and you need to perform IE - setting too.
var folder = "C:\\testing";
var fso = new ActiveXObject("Scripting.FileSystemObject");
if (!fso.FolderExists(folder)) {
fso.CreateFolder(folder);
}
fso=null;
It's not possible with other Browsers! And that's good!

ActiveXObject FileSystemObject not releasing in Javascript

I have a Javascript function that saves JSON data locally, using an ActiveXObject in IE9. It links into FileSystemObject or FSO scripting for file access.
If this Javascript function is run more than once, I get an error in IE debugger: "SCRIPT70: Permission denied" pointing to ts = savefile.OpenAsTextStream(2);
Why will it run just fine the first time, but not after that? My best guess is that something's not being released properly, although I can find no information on MSDN (or here).
Here's the code:
function SaveMyJSON() {
var ts;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var savefilepath = "C:\\MyFolder\\saveFile.json"
var savefile = fso.GetFile(savefilepath);
// open for writing only, value 2, overwriting the previous
// contents of the file
ts = savefile.OpenAsTextStream(2);
var myTestJson = {"id1" : "one", "id2" : "two"};
// copy to json
ts.WriteLine(myTestJson);
ts.Close;
}
The Close method needs empty parenthesis after it, like so:
ts.Close();
Ref here for more info.

Getting 'Path not found' when using CreateTextFile

I am trying to create a textfile but I get a Path not found error.
What am I doing wrong?
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fo = fso.GetFolder("\\logs")
var a = fo.CreateTextFile("testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();
I am using javascript in IE8. Thanks!
You can try first making sure that the logs directory exists. Next, try including the entire path of the logs directory within the source. This is since the default path of FileSystemObject is the current working directory.
For example, if logs is under C:\logs\
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fo = fso.GetFolder("C:\\logs")
var a = fo.CreateTextFile("testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();

JavaScript: Read files in folder

EDIT: I'm trying to read all the files in a specific folder and list the files in there, not read the content of a specific file. I just tried to simply create an FileSystemObject and it doesn't do anything either. I show an alert (which pops up) beforfe making the FileSystemObject, and one after it (which isn't shown). So the problem is in simply creating the object.
Original:
I am trying to read all the files in a folder by using JavaScript.
It is a local HTML file, and it will not be on a server, so I can't use PHP I guess.
Now I'm trying to read all the files in a specific given folder, but it doesn't do anything on the point I make a FileSystemObject
Here is the code I use, The alert shows until 2, then it stops.
alert('1');
var myObject, afolder, date;
alert('2');
myObject = new ActiveXObject("Scripting.FileSystemObject");
alert('3');
afolder = myObject.GetFolder("c:\\tmp");
alert('4');
date = afolder.DateLastAccessed;
alert("The folder"+name+" is a temporary folder.");
Am I doing this the right way?
Thanks!
The method I found with a Google search uses HTML5 so if you are using a modern browser you should be good. Also the tutorial page seems to check if the browser you are using supports the features. If so you should be good to follow the tutorial which seems pretty thorough.
http://www.html5rocks.com/en/tutorials/file/dndfiles/
This solution only works on IE11 or older since it is MS based
<script type="text/javascript">
var fso = new ActiveXObject("Scripting.FileSystemObject");
function showFolderFileList(folderspec) {
var s = "";
var f = fso.GetFolder(folderspec);
// recurse subfolders
var subfolders = new Enumerator(f.SubFolders);
for(; !subfolders.atEnd(); subfolders.moveNext()) {
s += ShowFolderFileList((subfolders.item()).path);
}
// display all file path names.
var fc = new Enumerator(f.files);
for (; !fc.atEnd(); fc.moveNext()) {
s += fc.item() + "<br>";
}
return s;
}
function listFiles() {
document.getElementById('files').innerHTML = showFolderFileList('C:');
}
</script>
<input type='button' onclick='listFiles()' value='List Files' />
<div id="files" />

Categories

Resources