admin管理员组文章数量:1403438
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 this, but I am not sure is this what am I searching for.
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://munity.hpe./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.
Share Improve this question asked Apr 11, 2016 at 1:19 DzejolinaDzejolina 311 gold badge1 silver badge3 bronze badges 3- 1 I'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. – user4244405 Commented Apr 11, 2016 at 1:23
-
1
You can use
webkitRequestFileSystem
at chrome or chromium 13+ and bothwebkitRequestFileSystem
andnew File()
constructor at versions 38+ see stackoverflow./questions/36098129/… – guest271314 Commented Apr 11, 2016 at 1:34 - It's a client-side app. Thank you so much – Dzejolina Commented Apr 11, 2016 at 13:34
1 Answer
Reset to default 3Sources
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
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
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 acplish 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.
本文标签: How can I create new folderfile with javascriptStack Overflow
版权声明:本文标题:How can I create new folderfile with javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744421915a2605500.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论