node.js - How to find payload in hapi -
i have following route in hapijs server. , trying create new file using ajax.
{ method: 'post', path: '/create', config : { payload:{ maxbytes: 10*1024*1024, output:'stream', parse: true, allow: 'multipart/form-data' }, handler: function (request, reply) { var data = request.payload; if (data.file) { // undefined var name = data.file.hapi.filename; var path = writepath + '/' + name; var file = fs.createwritestream(path); file.on('error', reply); data.file.pipe(file); data.file.on('end', function (err) { reply({ filename: data.file.hapi.filename, headers: data.file.hapi.headers }); }); } else reply(boom.badrequest('no file found. please try again.')); } }
the above code give data.file undefined. missing?
in hapi documentation http://hapijs.com/api#requests, when output stream
'stream' - incoming payload made available via stream.readable interface. if payload 'multipart/form-data' , parse true, fields values presented text while files provided streams. file streams 'multipart/form-data' upload have property hapi containing filename , headers properties.
html code :
<form enctype="multipart/form-data" action="/create" method="post"> <input type="file" id="uniquefileimporter"/> <input type="submit"/> </form>
of course there no js code, need submit form after selecting file system
thanks matt harrison pointing out mistake, missing attribute name in file inputer.
html should be
<form enctype="multipart/form-data" action="/create" method="post"> <input type="file" name="file" id="uniquefileimporter"/> <input type="submit"/> </form>
Comments
Post a Comment