How to create new local folder/directory in javascript - javascript

I want to create new folder locally using javascript in firefox browser. Following piece of code I tried, but no sucess.
var dir = new Dir(rcDirUrl); //doesnt work
dir.create(dir.DIRECTORY_TYPE, 0775);
Can anyone help out how to create directory in javascript.

if you're executing the JS in the browser - you cannot do it. Browser-based JS doesn't have access to the hard drive at all. You won't open anything (unless file is provided through file selector in HTML) and you won't store anything.
If you're using Node.js, you can do it with fs.mkdir() or fs.mkdirSync() - https://nodejs.org/api/fs.html#fs_fs_mkdirsync_path_mode

Related

How To Access Local File System With Chrome Extensions

I am trying to build a Chrome extension but I have encountered a problem. I need access to a file that is in the same directory as my JS script. Here is the representation:
 my_script.js
 My Folder
  |-> my_file.file
I use a third party library that does some required things with it. And I need to pass in the path to my_file.file The problem is my_script.js gets injected into the website I am trying to change and the code instead of searching on the local file system it tries to find it in www.website.com/My Folder/my_file.file which obviously does not exist. How can I make it so that it seacrhes relative to my_script.js? I unfortunately can't pass the file itself to func(), it has to be the path of the file. The file I am trying to reach is about 200MB which I was planning on shipping with my extension. Also this project is vanilla JS. Thank you!
my_script.js
...
lib.func('My Folder/my_file.file').then(function (out) {
document.out = out;
});
...

Javascript to read Text File to Array without a Webserver or external libraries local on Windows PC

I have an HTML file with JavaScript that I am running without any Webserver/host so I am just opening the file in a browser local to my windows PC. In that HTML file I would like to be able to read a text file in the same folder as the html file. That file will contain data in rows and columns separated with tabs. i.e
1 a
2 b
3 c
I want to keep this as simple as possible so all I have to do is share the HTML and Text file to others so the can open it up local to their computer without any webserver/host and without having to also copy of external libraries like node.js or jquery.
I have searched and tested everything I can find but either I need to reference an external library or I have to run it in a webserver or I need to click a button to load the file through the browser, none of what I want.
Does native JavaScript support the function to read a text file and save it to an array? If so, any code direction would be great.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
XMLHttpRequest() exists in native JavaScript, I think it will help you.
You also can send a request to the file. Or use library: axios.js because when you use XMLHttpRequest() you lose many time to write code which just get content from file, with axios I got file content with one line: `axios.get('file.txt').then(result => console.log(result.data));
To connect Axios: <script src="https://unpkg.com/axios#0.18.0/dist/axios.min.js"></script>
You can read official documentation about axios.js and XMLHttpRequest() in the net.

Android variable to JavaScript

I am building an Android WebView which loads an url on a server of mine. However, now I want to use an identifier from the Android as parameter of a function on the JS code and I simply can't do it.
I already tried appending "javascript:myFunction(myParameter);" to the url on the loadUrl method and tried adding a JavascriptInterface. Neither of them did work, and it looked like it needed my files (html and js) to be local. However, I need to open a xml document and, as far as I know, you can't do it with local files, so I couldn't test it.
I am not used to web/android dev, so I don't even know if I'm not trying to do something dumb, but it seems to me that it should be simple to do and yet I'm stuck on it. Thanks in advance.
Add the variable to the URL, for example:
file://my/file.html?variable=someValue
You should then be able to parse the URL in JS using document.location, for example:
alert(document.location);

Read/Write to JSON file in root directory of simply markup project

I have a very simple project. It's only local and I don't require it to ever be online. I'm perfectly content running it by opening index.html in chrome. Along with my html, css, and javascript file there is a data.JSON in my root directory of my project. My question is: can I read and write to this file? Or is there a possible alternative to simple data persistence. I've had no luck beating cross origin policy up to this point using $.getJSON.
$(document).ready(function(){
var data;
var g = $.getJSON('../data.JSON', function(d){
console.log(d);
data = d;
});
console.log(data)
d = proper JSON object,
data = undefined
I have no idea why...
javascript in your case is working only on client side and you cannot modify any file on client's machine.
Alternative solutions:
Use a server, if you are familiar with javascript, you can use node.js. Though it has a learning curve and you might take some time in learning that.
Download updated JSON file and replace original file with this new file. you can create files in javascript and download them on client machine. Then you have to manually replace original file.

How to Create a New Folder for Downloading a File

I want to download folder that client side (browser) make.
I considered using File API, but I can't find how to make folder.
For example, some png make from html canvas put one folder, I want to save this folder with download dialog.
I want to use a folder for some file to one folder.and I must download only once.
Please advise me.
html
<a download='folder' href='#' onclick="Download()">download</a>
You can use FSO.js, but be warned that it currently only works in Chrome. Additionally, the folders you create can only really be accessed from within your webapp (for example, you can't choose to write a specific folder on the C drive).
Firstly, You can't really interact with the local system or the server with JavaScript because of security. You can call a server-side script with JavaScript, via AJAX.
If you don't care, you can check out Javascript FSO CreateFolder Method
JavaScript Example...
// initialize ActiveXObject and create an object of Scripting.FileSystemObject.
var fso = new ActiveXObject("Scripting.FileSystemObject");
// creates a folder with specified name at the specified location
fso.CreateFolder("C:\\Temp\\myFolder");
fso = null;

Categories

Resources