在javaScript中使用提取API进行file upload

嗨,我试图从这么多天上传一个tar文件,但每次我失败了。 我是这个JS,UI和https req / res域的新手。 我的环境是:nodeJS,后端JavaScript。 ReactJS为前端。 我需要从我的机器上传tar文件到远程服务器。

这里是我的前端代码,我正在提取上传文件的请求。

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()); } 

这里是我的后端代码,我期待的tar文件接收。

 router.post('/policy', function(req, res) { console.log('hey I reached here'); console.log('hemant req',req.body.formData); // prints hemant req undefined // Dont know how to save it to some directory. ??? :( }); 

我的整个想法是将tarfile upload到位于远程位置的服务器。 我尝试了很多其他方法,但似乎没有任何帮助。

body-parser模块仅处理JSON和urlencoded表单提交,而不是多部分(如果您正在上传文件,将会是这种情况)。

对于多部分,你需要使用像connect- busboy或者multer或者connect-multiparty (multiparty /强大的就是最初在expressParser中间件中使用的东西)。