Avatar for Wilberforce

Wilberforce

Member since Feb 2016 • Last active Mar 2017
  • 16 conversations
  • 362 comments

Most recent activity

  • in JavaScript
    Avatar for Wilberforce
    
    /*
    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/li­bs/jquery/2.1.4/jquery.min.js"></script>­
    <script src="https://cdnjs.cloudflare.com/ajax/l­ibs/twitter-bootstrap/3.3.7/js/bootstrap­.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/­libs/twitter-bootstrap/3.3.7/css/bootstr­ap.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­'));
    
    
  • in JavaScript
    Avatar for Wilberforce

    Thanks Gordon. Decoding the post boundarys is quite difficult so I decided to do it the easy way - put the file content as the body and just stream that up.

    So this lets the browser do all the hard work instead... It allows you to select a file and upload it using the PUT method... insteading pasting the file contents. It should allow binary files too.

    Next steps are to clean up the look, and allow folder creation and folder selection.....

  • in JavaScript
    Avatar for Wilberforce

    I'm looking at using the file upload input element with the multi-part encoding, so that files can be uploaded including binary files.

    Can you think of a cunning way of decoding the multi-part message?

  • in JavaScript
    Avatar for Wilberforce

    @gordon any chance you could post your code - I would like to see where you put the end handler.

  • in ESP8266
    Avatar for Wilberforce

    Sorry - the esp8266 does not support https:// . The TLS library has not been compiled into the firmware, as the secure protocol needs more ram buffer space than the 8266 has...

    It does work on a espruino wifi or the esp32.

  • in ESP8266
    Avatar for Wilberforce

    Did you modify the connection set up?

    This example works on the esp32, you might have enough space in the esp8266...

    It assumes you have a wifi connected, and wifi.save() already done so that there is a connnection:

    github.com/espruino/Espruino/blo­b/master/targets/esp32/tests/https-googl­e-sheets.js

  • in JavaScript
    Avatar for Wilberforce

    @JumJum
    The best thing to do is to use the window 10 64 bit bash on windows. I'm using this for development.
    Install Bash on Ubuntu on Windows 10 msdn.microsoft.com/da-dk/command­line/wsl/install_guide

    git clone  --recursive  git://github.com/espruino/EspruinoBuildT­ools.git
    cd EspruinoBuildTools/esp32/build/Espruino
    
    source scripts/provision.sh LINUX
    make clean && make
    ./espruino
    
  • in JavaScript
    Avatar for Wilberforce

    Is this because C5 has special meaning? Would it work with an int or string?

  • in ESP32
    Avatar for Wilberforce

    If your join the esp32 to wifi, you can then enter it's pi address in
    settings->communications, and then connect to it via wifi.

    Upload of code is much faster too!

Actions