Im receiving a buffer from a post request in the form-data (which is being handled by multer). And i need to pass that along to another service by making post request using form-data.
const documentFile = req.body.image;
const image = bufferToStream(documentFile.buffer);
function bufferToStream(binary) {
const readableInstanceStream = new Readable({
read() {
this.push(binary);
this.push(null);
},
});
return readableInstanceStream;
}
image
is now a Readable rather than a ReadStream you would get from fs.createReadStream(). I need a readstream.
So i can use axios to send a form-data request like this.
const formData = new FormData();
formData.append("image",image);
const options: any = {
url,
method: "post",
headers: formData.getHeaders(),
data: formData,
config: retryConfig,
};
await axios(options)
After getting file from multer
, you can do exactly the same on server as if you were sending from client. Install form-data
package and try below snippet.
const FormData = require('form-data');
...
const formData = new FormData();
formData.append("image", req.body.image);
axios({ data: formData });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.