I want to draw structure of some chemicals based on values in a log file . So I write a javascript for drawing and it works for predefined x,y,z coordinates value
.txt file contain
its 5th,6th,7th indicates x,y,z coordinates value.
log file contain following fields
is there is any way to browse a file from user and accept those value in 5th,6th,7th field of .txt file.
To get the file uploaded check out kk anu's answer.
Once you have the file in access and can read it, it's a straight forward iteration of each line.
var linesFromFile= file.split(/[ ,]+/);
linesFromFile.forEach(function(entry)
{
var parsedFloat = parseFloat(entry);
if(parsedFloat === Number(parsedFloat) && parsedFloat%1!==0){
// do something with the parsed float
}
});
You have to adapt the code for your needs, but this is basically the function to go with.
Related
I am currently on a Chromebook using HTML, CSS, JS on repl.it, and I can't find a way to check the files in a folder.
I have a folder called objects containing a bunch of JSON files. I want the object dataQueue to hold every object, and I have got that working. But I don't want to have to specifically type out each file to make it run the function I made called getFile for every single file, as is done below in the code.
I am also trying to do this without any libraries (so I can understand everything), and also I want it to be able to run both as a site on the computer and on repl.it.
This is my current code:
//defined the dataQueue object where the object files
//will be stored
var dataQueue={};
//a function to get files by just inputting their
//filename and what i want to call them with in the future
function getFile(theFile,index){
//sets to a new request
var txtFile = new XMLHttpRequest();
//works with any file type, gets the specified file
txtFile.open("GET", theFile, true);
//I don't know what this does
txtFile.send(null);
//runs when the file is recieved
txtFile.onreadystatechange = function(){
//test to make sure it is ready
if(txtFile.readyState==4){
//add object file to dataQueue object
dataQueue[index]=JSON.parse(txtFile.response);
}
}
}
//get each file individually
getFile("objects/ship1.json","ship1");
getFile("objects/box1.json","box1");
getFile("objects/bigplatform1.json","platform1");
getFile("objects/bigbox1.json","bigbox1");
I want to be able to just add another JSON file to the objects folder and have it put in the dataQueue object without needing to add another getFile function.
I think a solution would be if I could somehow get a list of strings containing all of the filenames in the Objects folder, but I have tried finding a way to, and I couldn't find anything.
The question: How do I get each file name in a specific folder in a website's files?
I am using dygraphs with a CSV file that is constantly having data added to it. I would like to use dateWindow to have it auto zoom in on a range that is from a date I specify to the end of the file when it loads. I can't make it work.
I've tried just leaving the "latest" value blank such as:
dateWindow: [Date.parse("2017/07/01 12:00:00"),],
dateWindow: [Date.parse("2017/07/01 12:00:00"), Date.parse("")],
but nothing works. Is this possible and if so can someone point me to how to do it? Thank you.
You can retrieve the x value range with g.xAxisExtremes() once the csv data values are loaded. Then call g.updateOptions() passing dateWindow as your desired starting date and the high end of the value range. The csv file is loaded dynamically, so you'll want to set the desired range in a ready() function.
Something like this:
<script type="text/javascript">
var g = new Dygraph(
document.getElementById("graphdiv"),
"temperatures.csv", // path to CSV file
{} // options
);
g.ready(function () {
var ends = g.xAxisExtremes();
g.updateOptions({dateWindow: [Date.parse("2017/07/01 12:00:00"),ends[1]]});
});
</script>
in extendscript - Photoshop, I would like my dialog-box check boxes to default to previously used choices ... anyone know if this is possible?
You have two choices.
First choice: Using a //#targetengine
Values can be made persistent over a session using a targetengine.
First script
//#targetengine myengine
var x = 100;
Second script
//#targetengine myengine
$.writeln(x);
If you close Photoshop all of the values will be lost
Second choice: Write to a file.
I wont write an example here. This can be done in so many ways. Plain .txt file. .json file. See this example on how to read and write files.
fabianmoronzirfas has got the right answer.
I will say it could with one script only. That script reads in the previous value stored in a text file in a hardcoded location like C:\temp. If the script cannot file the settings file it'll default to some predetermined value and then store this time around.
Just in case, here is the simple script that saves (and tries to load) your prefs in JSON format to the system temp folder:
// set default values
var prefs = {
file: File(Folder.temp.fsName + "/prefs.json"),
title: "",
length: 0
}
// try to load previous prefs
if (prefs.file.exists) prefs = $.evalFile(prefs.file);
// do something
prefs.title = prompt("Type the title:", prefs.title);
prefs.length = prefs.title.length;
// save the prefs to the file
prefs.file.open("w");
prefs.file.write(prefs.toSource());
prefs.file.close();
Is it possible to get a fileEntry object in Chrome Apps by opening a file via Drag'n'Drop? When I drop a file into my app I only get a file object which seems to be unrelated to the file system. I can't use that object to save the file after changing it.
I get the file like this:
document.body.addEventListener('drop', function (event) {
file = event.dataTransfer.files[0]
});
What I want to do
I'm developing a text editor and I want to add a feature to open a file by dragging it into my app.
As I said: I already get the content of the file, but I can't write changes back to the file since I need a fileEntry object in order to do so.
Okay, I just found it while inspecting the event object. In the event object there's a function called webkitGetAsEntry() to get the fileEntry object. Here's the correct code:
document.body.addEventListener('drop', function (event) {
fileEntry = event.dataTransfer.items[0].webkitGetAsEntry();
});
This is the object you can use to write changes back to the file system.
Code for reference:
// Of course this needs the "fileSystem" permission.
// Dropped files from the file system aren't writable by default.
// So we need to make it writable first.
chrome.fileSystem.getWritableEntry(fileEntry, function (writableEntry) {
writableEntry.createWriter(function (writer) {
// Here use `writer.write(blob)` to write changes to the file system.
// Very important detail when you write files:
// https://developer.chrome.com/apps/app_codelab_filesystem
// look for the part that reads `if (!truncated)`
// This also is very hard to find and causes an annoying error
// when you don't know how to correctly truncate files
// while writing content to the files...
});
});
I am creating a web portal where end user will upload a csv file and I will do some manipulation on that file on the server side (python). There is some latency and lag on the server side so I dont want to send the message from server to client regarding the bad format of uploaded file. Is there any way to do heavy lifting on client side may be using js or jquery to check if the uploaded file is "comma" separated or not etc etc?
I know we can do "accept=.csv" in the html so that file extension has csv format but how to do with contents to be sure.
Accessing local files from Javascript is only possible by using the File API (https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications) - by using this you might be able to check the content whether it matches your expectations or not.
Here's some bits of code I used to display a preview image clientside when a file is selected. You should be able to use this as a starting point to do something else with the file data. Determining whether its csv is up to you.
Obvious caveat:
You still have to check server side. Anyone can modify your clientside javascript to pretend a bad file is good.
Another caveat:
I'm pretty sure that you can have escaped comma characters in a valid csv file. I think the escape character might be different across some implementations too...
// Fired when the user chooses a file in the OS dialog box
// They will have clicked <input id="fileId" type="file">
document.getElementById('fileId').onchange = function (evt) {
if(!evt.target.files || evt.target.files.length === 0){
console.log('No files selected');
return;
}
var uploadTitle = evt2.target.files[0].name;
var uploadSize = evt2.target.files[0].size;
var uploadType = evt2.target.files[0].type;
// To manipulate the file you set a callback for the whole contents:
var FR = new FileReader();
// I've only used this readAsDataURL which will encode the file like data:image/gif;base64,R0lGODl...
// I'm sure there's a similar call for plaintext
FR.readAsDataURL($('#file')[0].files[0]);
FR.onload = function(evt2){
var evtData = {
filesEvent: evt,
}
var uploadData = evt2.result
console.log(uploadTitle, uploadSize, uploadType, uploadData);
}
}