Your example was on my todo list, excuse the long post.  I found the
diagnostic logging helpful in understanding the npm upload package busboy,
but you can easily shorten it  Using "~/mytmp/" as my temporary directory
and tweaking the example at https://www.npmjs.com/package/busboy my
server.js file is:

var http = require('http'),
    path = require('path'),
    os = require('os'),
    fs = require('fs');
    inspect = require('util').inspect;

var Busboy = require('busboy');

http.createServer(function(req, res) {
  if (req.method === 'POST') {
    var busboy = new Busboy({ headers: req.headers });

    busboy.on('file', function(fieldname, file, filename, encoding,
mimetype) {
      console.log('File [' + fieldname + ']: filename: ' + filename + ',
encoding: ' + encoding + ', mimetype: ' + mimetype);
      // uncomment next line to use system temporary directory
      // var newPath = os.tmpDir();
      var newPath = __dirname + "/mytmp";
      var saveTo = path.join(newPath, path.basename(filename));
      file.pipe(fs.createWriteStream(saveTo))
      file.on('data', function(data) {
        console.log('File [' + fieldname + '] got ' + data.length + '
bytes');
      });
      file.on('end', function() {
        console.log('File [' + fieldname + '] Finished');
      });
    });

    busboy.on('field', function(fieldname, val, fieldnameTruncated,
valTruncated, encoding, mimetype) {
      console.log('Field [' + fieldname + ']: value: ' + inspect(val));
    });

    busboy.on('finish', function() {
      console.log('Done!');
      res.writeHead(303, { Connection: 'close', Location: '/' });
      res.end();
    });

    return req.pipe(busboy);

  } else if (req.method === 'GET') {

    res.writeHead(200, { Connection: 'close' });
    res.end('<html><head></head><body>\
               <form method="POST" enctype="multipart/form-data">\
                <input type="text" name="textfield"><br />\
                <input type="file" name="filefield"><br />\
                <input type="submit">\
              </form>\
            </body></html>');
  }
}).listen(8000, function() {
  console.log('Listening for requests');
});

---------------

server.js generates a simple webpage:
 <html>
  <head>
  </head>
  <body>
     <form method="POST" enctype="multipart/form-data">
       <input type="text" name="textfield"><br />
       <input type="file" name="filefield"><br />
       <input type="submit">
     </form>
   </body>
</html>

-------------

// Example output, using can-bus.jpg as the test file:
//
//Listening for requests
// Field [textfield]: value: ''
// File [filefield]: filename: can-bus.jpg, encoding: 7bit, mimetype:
image/jpeg
// File [filefield] got 52320 bytes
// File [filefield] got 65536 bytes
// <snip ...>
// File [filefield] got 4505 bytes
// File [filefield] Finished
// Done!

-------------------------------------------------
Paul Wolfson, Ph.D., TX LPI, #A17473
Dallas Legal Technology
3402 Oak Grove Avenue, Suite 300-A
Dallas, Texas 75204-2353


*214-257-0984 (Tel)214-838-7220 (Fax)Send me an email. <[email protected]>*
-------------------------------------------------
The contents of this email are confidential to the sender and the ordinary
user of the email address to which it was addressed, and may also be
privileged.  If you are not the addressee of the email, you may not copy,
forward, disclose or otherwise use it or any part of it in any form
whatsoever.  If you have received this email in error, please advise the
sender at  214-257-0984.  Thank you.
-------------------------------------------------

On Sat, Mar 26, 2016 at 12:28 AM, <[email protected]> wrote:

> I have a simple need but in node.js I know it can't be done do to security.
> b.writeTextFile does not seem to work either.
>
> My need is for a user to input a file name and have this file name saved
> on the server for future append to file.
> Like saving encoder values.
>
> This will log the user input but "Cannot read property 'writeFile' of
> undefined"
>
> If anyone has any ideas please help.
>
> var fs = require('fs');var input = document.getElementById('filename');var 
> fileName = input.value;
> function recordToFilename() {var input = 
> document.getElementById('filename');var fileName = input.value;var qw = 
> fileName;
> if (qw) {
>     alert('value of: ' + qw);
>     console.log(qw);
>     // demo output
>     var myObject = {
>         qw: qw,
>         fullN: function() {
>             return this.qw;
>         }
>     };
>
>     document.getElementById("demo").innerHTML = myObject.fullN();
>     var path = "danny.txt";
>     var data1 = "jdsfhadj"
>     fs.writeFile(path,data1)
>     //end demo output code
>     } else {
>         alert('Please enter a filename!');
>         input.focus();
>     }};
>
> HTML Code:
>
> <html><head>
>     <title>writeFile</title>
>     <script src="jquery.js"></script>
>     <script src="bonescript.js"></script>
>     <script src="test_3.js"></script></head><body>
>
>     <label for="filename">Filename</label>
>     <input name="filename" id="filename" type="text">
>
>     <a id="enter_button" onclick="recordToFilename();"    
> href="javascript:void(0);" title="enter">enter name</a>
>     <br>
>     <p id="demo"></p></body></html>
>
>
>
> --
> For more options, visit http://beagleboard.org/discuss
> ---
> You received this message because you are subscribed to the Google Groups
> "BeagleBoard" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
For more options, visit http://beagleboard.org/discuss
--- 
You received this message because you are subscribed to the Google Groups 
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to