node.js - file upload using fetch API in javaScript -
hi trying upload tar file many days, everytime failing. new js, ui , https req/res domain. environment is: nodejs, javascript backend. reactjs frontend. need upload tar file machine remote server.
here frontend code making fetch request upload file.
export function postxccdf () { let _headers = { 'accept': 'application/json, application/xml, text/plain, text/html, *.*', 'content-type': 'multipart/form-data; boundary=----webkitformboundaryyemkndsbkjb7qequ' }; var formdata = new formdata(); formdata.append('type', 'file'); formdata.append('file', 'c:\\mydrive\\rules.zip'); let uri = '/rest/policy'; const options = { method: 'post', headers: _headers, body: formdata }; return fetch(`${_host}${urlprefix}${uri}`, options) .then(processstatus) .then(response => response.json()); }
here backend code , expecting tar file receive.
router.post('/policy', function(req, res) { console.log('hey reached here'); console.log('hemant req',req.body.formdata); // prints hemant req undefined // dont know how save directory. ??? :( });
my whole idea upload tar file server located @ remote location. have tried lot many other ways, nothing seems help.
the body-parser module handles json , urlencoded form submissions, not multipart (which case if you're uploading files).
for multipart, you'd need use connect-busboy or multer or connect-multiparty (multiparty/formidable used in express bodyparser middleware).
Comments
Post a Comment