const FormData = require("form-data"); const axios = require('axios') const decodeContent = content => { let lines = content.split('\n'); const findFlagNo = (arr, flag) => arr.findIndex(o => o.includes(flag)); // find ----- content-disposition content-type position and remove const startNo = findFlagNo(lines, '------'); lines.splice(startNo, 1); const ContentDispositionNo = findFlagNo(lines, 'Content-Disposition'); lines.splice(ContentDispositionNo, 1); const ContentTypeNo = findFlagNo(lines, 'Content-Type'); lines.splice(ContentTypeNo, 1); Const endNo = lines.length-findFlagno (lines.reverse(), '------') -1; const endNo = lines.length-findFlagno (lines.reverse(), '------') -1; Lines.reverse ().splice(endNo, 1); return Buffer.from(lines.join('\n')); } const arr = [] req.on('data', (buffer) => { arr.push(buffer); }) req.on('end', async () => { let buffer = Buffer.concat(arr); const content = buffer.toString(); const result = decodeContent(content); const fileName = content.match(/(? <=filename=").*? (? /) [0] = "); const fileContent = Buffer.from(result); let formData = new FormData(); formData.append('file', fileContent, { filename: fileName, contentType: 'text/plain', knownLength: fileContent.byteLength }); formData.append('filename', fileName); let headers = formData.getHeaders(); const uploadfile = await axios({ method: "post", url: "url", data: formData, headers }).then(data => data.data || {}) res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(uploadfile)) })Copy the code