• 
    /*
    Proof of concept fs upload file ESP32 / Linux
    (c) 2017 Wilberforce
    
    */
    
    function post_file(req,res){
      console.log("post_file",req.url);
      var f = E.openFile(req.url,"w");
      req.pipe(f,{chunkSize:512, end:false,
                  complete:function(){
                    console.log("Complete");
                    f.close();
                    res.end("pipe upload done");
                  }
                 });
      res.end();
    }
    
    
    var html_index=`
    <html>
    <head>
    <title>Uploader</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
    
    <form action="" method="post">
        <input type="file" name="user_file" />
        <button type="submit">Submit</button>
    </form>
    
    <script>
    var ajaxFileUpload = function (filename,data) {
        var xhr = new XMLHttpRequest();
        xhr.open("PUT", "http://192.168.15.18:88/node_modules/"+filename, true);
        xhr.addEventListener("load", function (e) {
            // file upload is complete
            console.log(xhr.responseText);
        });
        //xhr.send(data);
     xhr.send(data);
    };
    
    var form = document.querySelector("form");
    //debugger;
    form.addEventListener("submit", function (e) {
    
    //debugger;
    var input = document.querySelector('input[type="file"]');
    var fdata = new FormData();
    var file;
    
        file = input.files[0];
        //fdata.append(file.name, file);
    
    //var file = $('#load-file')[0].files[0];
    //debugger;
      var fileReader = new FileReader();
      fileReader.onloadend = function (e) {
        var arrayBuffer = e.target.result;
        var fileType = $('#file-type').val();
    
      //debugger;
      ajaxFileUpload(file.name,arrayBuffer);
    
      };
      fileReader.readAsArrayBuffer(file);
    
        // Prevents the standard submit event
        e.preventDefault();
        return false;
    }, false);
    </script>
    
    </html>
    `
    
    function page_index(req,res) {
      res.writeHead(200);
      res.end(html_index);
      return 200;
    }
    
    var http = require("http");
      http.createServer(function (req, res) {
        console.log({header:req.header});
        console.log({method:req.method});
        console.log({url:req.url});
        code=404;
        if(req.url=='/' && req.method == 'GET') code=page_index(req,res);
        
        if(req.url=='/post' && req.method == 'POST') code=post_file(req,res);
        if(req.url=='/put' || req.method == 'PUT') code=post_file(req,res);
        if ( code == 404 ) {
          res.writeHead(404);
          res.end('Not Found');
        }
      }).listen(88); 
    // Not found
    
    fs=require("fs");
    if ( typeof(fs.readdirSync())==="undefined" ) {
      console.log("Formatting FS");
      E.flashFatFS({format:true});
    }
    console.log(fs.readdirSync());
    console.log(fs.readdirSync('node_modules'));
    
    
About

Avatar for Wilberforce @Wilberforce started