如何使用JavaScript创建新的文件夹/文件? [英] How can I create new folder/file with javascript?

查看:799
本文介绍了如何使用JavaScript创建新的文件夹/文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,然后进入需要在应用程序目录中创建新文件夹/文件的部分。我为这些文件/文件夹创建了表格,并创建了按钮,但我不确定它们的实际显示方式。
我找到了这个http://community.hpe.com/t5/HPE-Service-Manager-Service/Javascript-Create-New-Local-Folder/td-p/6768020,但是我不确定这是什么我在寻找。

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.

推荐答案


对于任一客户端的JavaScript解决方案,端或服务器端,可以使用Node.js;但是,

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

您可以找到有关这两个 JavaScript中任何一个的大量文档。解决方案;但是,还有其他 JavaScript可以使用NodeJS解决方案。

You can find extensive documentation on either of these "JavaScript" solutions; however, there are other "JavaScript" solutions available, NodeJS is very popular.

如果您在服务器上使用其他语言(如PHP),则可以在此处找到有关它的更多信息: http://php.net

If you work with another language on the server, like PHP, you can find more info about it here: http://php.net

以下内容描述了一个JavaScript解决方案,其中包含服务器端代码,您只需复制&粘贴并修改以满足您的需求。

The following describes a JavaScript solution with code for server-side that you can just copy & paste and modify to your needs.

这假定您在Linux上运行NodeJ,并且文件/文件夹(路径)不是递归的。以下示例未经测试,请随时进行测试&

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.

用于与服务器端交互的客户端代码。下面的示例创建一个HTML表单,该表单使用: method = PUT vars 所需的字段; -或者-使用AJAX方法来完成相同的操作。


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.

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);




用法


您可以从您的Web浏览器(如果服务器在同一台计算机上运行),则在Web浏览器的地址栏中键入: http://127.0.0.1:8124 并按Enter /返回;但是,请参阅正确的NodeJS文档,以提供必要的客户端HTML&如前所述的JavaScript。


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.

这篇关于如何使用JavaScript创建新的文件夹/文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆