javascript - Angular2 server-side-listener for file uploads -


in angular2 cli project, finnaly implemented this upload button vaadin. button ui works, don't know how make upload file anywhere.

i keep finding solutions express server listens file uploads, multer or node server, , have no idea how write such server, put it, how start it, how access it, etc.. figured trivial file upload should easier achieve, seems not.

what simple solution implement along side angular2 in order make button upload files somewhere can download them later?

found solution in ng2-uploader repo , adapted work vaadin upload.

component.html

    <div *ngif="newname.valid">             <vaadin-upload                      target="http://localhost:10050/upload"             </vaadin-upload>     </div> 

server.js

'use strict';  const hapi        = require('hapi'); const inert       = require('inert'); const md5         = require('md5'); const multiparty  = require('multiparty'); const fs          = require('fs'); const path        = require('path'); const server      = new hapi.server();  server.connection({ port: 10050, routes: { cors: true } }); server.register(inert, (err) => {});  const upload = {   payload: {     maxbytes: 209715200,     output: 'stream',     parse: false   },   handler: (request, reply) => {     const form = new multiparty.form();     form.parse(request.payload, (err, fields, files) => {       if (err) {         return reply({status: false, msg: err});       }        let responsedata = [];        files.file.foreach((file) => {         let filedata = fs.readfilesync(file.path);          const originalname = file.originalfilename;          const generatedname = md5(new date().tostring() +            originalname) + path.extname(originalname);          const filepath = path.resolve(__dirname, 'uploads',            originalname);          fs.writefilesync(filepath, filedata);         const data = {           originalname: originalname,           generatedname: generatedname         };          responsedata.push(data);       });        reply({status: true, data: responsedata});     });   } };  const uploads = {   handler: {     directory: {       path: path.resolve(__dirname, 'uploads')     }   } };  server.route([   { method: 'post', path: '/upload',          config: upload  },   { method: 'get',  path: '/uploads/{path*}', config: uploads } ]);  server.start(() => {   console.log('upload server running at', server.info.uri); }); 

and bonus need server.js running @ startup this awesome solution tested , working.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -