File is not defined in JavaScript - javascript

I'm writing a code in JavaScript the opens a file then read it:
var txtFile = "/dataset/happy.txt";
var fileH = new File(txtFile);
var fileS = new File("/dataset/sad.txt");
var fileA = new File("/dataset/angry.txt");
var fileSC = new File("/dataset/scared.txt");
var fileSU = new File("/dataset/suprised.txt");
fileH.open("r"); // open file with read access
But I get (ReferenceError: File is not defined) error
What's the problem?
Thank you :)

Related

reading data line by line from txt file using javascript Rhino

I have created a txt file using python code which contents like as shown in image:
I am writing a javascript code in a Nomagic Cameo system modeler . I am reading the file but I want to read all the given lines one by one into a variable and then use this variable to open these csv files in loop and do some processing.
So far I have written the following code but I am not sure how to correct it as per my requirement.
importPackage(org.w3c.dom);
importClass(java.io.File);
importClass(java.util.Scanner);
importClass (java.util.Scanner);
//importClass(java.io.BufferedReader);
importClass(javax.xml.parsers.DocumentBuilderFactory);
importClass(javax.xml.transform.OutputKeys);
importPackage(java.sql);
importClass(com.nomagic.magicdraw.automaton.AutomatonMacroAPI);
importClass(com.nomagic.magicdraw.openapi.uml.SessionManager);
java.lang.Class.forName("com.mysql.jdbc.Driver");
var conn = DriverManager.getConnection("jdbc:mysql://"+HostName+":"+Port,Username,Password);
var stat = conn.createStatement();
var resultSet = null;
var logger = com.nomagic.magicdraw.core.Application.getInstance().getGUILog();
var fileR = new File(FilePath);
//var sc = new Scanner(fileR);
var scanner = new Scanner(new File(fileR));
while (scanner.hasNextLine()) {
var line = scanner.nextLine();
print(line);
// process the line
}

how to use createAndFillWorkbook() in exceljs?

I followed the documentation
var workbook = createAndFillWorkbook();
and I get this
error Object # has no method 'createAndFillWorkbook'
even if I required exceljs already
var Excel = require("exceljs");
What I wanted to do was to create a report but I am somehow confused on the documentation because it does not say here how to use the createAndFillWorkbook() method it just says here to use it right away.
I referred here in the documentation: https://github.com/guyonroche/exceljs#writing-xlsx
createAndFillWorkbook(); does not mean a function.(maybe pseudo function)
You must create some workbook then fill content.
See below.
// create workbook by api.
var workbook = new Excel.Workbook();
// must create one more sheet.
var sheet = workbook.addWorksheet("My Sheet");
// you can create xlsx file now.
workbook.xlsx.writeFile("C:\\somepath\\some.xlsx").then(function() {
console.log("xls file is written.");
});
var excel = require("exceljs");
var workbook1 = new excel.Workbook();
workbook1.creator = 'Me';
workbook1.lastModifiedBy = 'Me';
workbook1.created = new Date();
workbook1.modified = new Date();
var sheet1 = workbook1.addWorksheet('Sheet1');
var reColumns=[
{header:'FirstName',key:'firstname'},
{header:'LastName',key:'lastname'},
{header:'Other Name',key:'othername'}
];
sheet1.columns = reColumns;
workbook1.xlsx.writeFile("./uploads/error.xlsx").then(function() {
console.log("xlsx file is written.");
});
This is my sample code which works for me.

Creating a Parse.File Object

Loving parse at the moment just cant seem to upload a file to parse using the javascript api (client side), the issue is I can't seem to create a parse object. The following is my code:
var fileUploadControl = $("#imgInp")[0];
var file = fileUploadControl.files[0];
var name = "photo.jpg";
var parseFile = new Parse.File(name, file);
parseFile.save().then(function() {
// etc
The Error I'm getting from the console on chrome is
TypeError: undefined is not a function
at k.$scope.submitReport
(file:///Users/xxx/Documents/projects/script.js:402:27) <- the line which has var parseFile = new Parse.File(name, file); on it
Could anybody shed some light on this bizarre situation (I'm fairly certain I'm following the documentation to the letter.
https://parse.com/docs/js_guide#files-classes
you should try with encoding in base 64, it works finally:
var fileUploadControl = $("#imgInp")[0];
var file = fileUploadControl.files[0];
var uploadFile = new Promise(function(resolve, reject){
var newFile = new Parse.File(String(file.name),{base64:file.image});
newFile.save();
resolve(newFile)
})
uploadFile.then(function(){
var results = Array.prototype.slice.call(arguments);
//results === your filesUpload
});

Writing json string into file using Windows Script Host

I am trying to write json string that I stringified using https://github.com/douglascrockford/JSON-js/blob/master/json2.js ( JSON.stringify) ,
I am trying to save the string and I am getting an error:
Microsoft JScript Runtime Error: Invalid procedure call or argument
at
var textStream = fileObj.OpenAsTextStream(ForWriting, TristateFalse);
this is my code :
var pref = JSON.parse(textPref);
textPref = JSON.stringify(pref);
WSH.echo(textPref)
// Create the new file.
fso.CreateTextFile("d:\\Preferences_temp", true);
var fileObj = fso.GetFile("d:\\Preferences_temp");
var textStream = fileObj.OpenAsTextStream(ForWriting, TristateFalse); <- Microsoft JScript Runtime Error: Invalid procedure call or argument
textStream.Write(textPref);
textStream.Close();
The constants you pass to the FSO methods are not exposed via late binding with new ActiveXObject so you need to define them independently;
var ForWriting = 2;
var TristateFalse = 0;
Edit
var ForWriting = 2;
var TristateTrue = -1;
var textPref = '{"xxx": "AA \u05D5 BB"}';
var pref = JSON.parse(textPref);
textPref = JSON.stringify(pref)
// Create the new file.
var fso = new ActiveXObject("Scripting.FileSystemObject");
//create as unicode
fso.CreateTextFile("c:\\null\\Preferences_temp", true, true);
var fileObj = fso.GetFile("c:\\null\\Preferences_temp");
//open for unicode
var textStream = fileObj.OpenAsTextStream(ForWriting, TristateTrue);
textStream.Write(textPref);
textStream.Close();

Jscript ReadLine() related

Can any one please tell me that we use ReadLine() to read a particular line from a file (.txt). Now I want to read the total content of the file (not only the first line). For that what method I need to use. I googled a lot but I cant get the solution.
My Code is given below:
var ForReading = 1;
var TristateUseDefault = -2;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var newFile = fso.OpenTextFile(sFileName, ForReading, true, TristateUseDefault);
var importTXT = newFile.ReadLine();
This is returning the first line of the .txt file by importTXT variable. Now I want to get the total file content in importTXT.
Any suggestion will be very much helpful for me.
You use the ReadAll method:
var importTXT = newFile.ReadAll();
(Don't forget to close the stream when you are done with it.)
Here: ReadAll (msdn)
I found the example given very poor - for example it did not CLOSE the file, so I added this to the msdn page:
function ReadAllTextFile(filename)
{
var ForReading = 1;
var fso = new ActiveXObject("Scripting.FileSystemObject");
// Open the file for input.
var f = fso.OpenTextFile(filename, ForReading);
// Read from the file.
var text = (f.AtEndOfStream)?"":f.ReadAll(); // this is where it is read
f.Close();
return text;
}
var importTXT = ReadAllTextFile(sFileName);

Categories

Resources