I am building an app, and I got to the part where I need to create new folder/file in directory that is in the app. I made forms for those files/folders, and buttons to create them,but I'm not sure how they actually appear. I found thishttp://community.hpe.com/t5/HPE-Service-Manager-Service/Javascript-Create-New-Local-Folder/td-p/6768020, but I am not sure is this what am I searching for.
-
1I'm assuming that you want to create the file/folder (path) on the server of the app? - or is this client side? Which ever it is, both is possible, I just need to know which and I'll post an answer. – Apr 11 '16 at 01:23
-
1You can use `webkitRequestFileSystem` at chrome or chromium 13+ and both `webkitRequestFileSystem` and `new File()` constructor at versions 38+ see http://stackoverflow.com/questions/36098129/how-to-write-in-file-user-directory-using-javascript/ – guest271314 Apr 11 '16 at 01:34
-
It's a client-side app. Thank you so much – Dzejolina Apr 11 '16 at 13:34
1 Answers
Sources
For a JavaScript solution for either client-side or server-side, you can use Node.js; however,
- client side requires a package called: "NWJS" available here: http://nwjs.io/
- server side only requires "Node.js" available here: https://nodejs.org
You can find extensive documentation on either of these "JavaScript" solutions; however, there are other "JavaScript" solutions available, NodeJS is very popular.
If you work with another language on the server, like PHP, you can find more info about it here: http://php.net
Solution
The following describes a JavaScript solution with code for server-side that you can just copy & paste and modify to your needs.
This assumes you are running NodeJs on Linux and that the file/folder (path) is not recursive. The example below is not tested, feel free to test & fix as necessary.
For the client-side code interacting with the "server-side" example below, create an HTML form that uses: method="PUT"
and the fields as required by the vars
; -OR- use an AJAX method to accomplish the same.
Server-side: NodeJS
let http = require('http');
//File System package...
let fsys = require('fs');
let makePath = function(root, path, data)
{
try
{
fsys.accessSync(root, fsys.W_OK);
}
catch(err)
{
return {code:403, text:'Forbidden'}
}
path = ((path[0] == '/') ? path.substr(1, path.length) : path);
if (path.split('/').length > 2)
{ return {code:412, text:'Precondition Failed'}; }
if (fsys.existsSync(path))
{ return {code:409, text:'Conflict'}; }
if (path[path.length -1] == '/')
{ fsys.mkdirSync(root +'/'+ path.substr(0, path.length -2)); }
else
{ fsys.writeFileSync((root +'/'+ path), (data || ' '), 'utf8'); }
return {code:200, text:'OK'};
};
http.createServer
(
function(request, response)
{
let vars = url.parse(request.url);
if (path && (path.indexOf('/') > -1) && (request.method == 'PUT'))
{
var resp = makePath(__dirname, vars.path, vars.data);
response.statusCode = resp.code;
response.setHeader('Content-Type', 'text/plain');
response.end(resp.text);
}
}
).listen(8124);
Usage
You can access this from your web browser, if your server runs on the same machine, in your web-browser's address bar type: http://127.0.0.1:8124
and hit enter/return; however, please see the proper NodeJS documentation for serving the necessary client-side HTML & JavaScript as mentioned.

- 1
- 1