admin管理员组文章数量:1277269
I want to extract the binary content from a multipart response that I have as follows -
I do not have access to any request object (Express/NodeJS). I just have this response object which I want to parse and extract the binary part of it.
EDIT -
To make things more clear -
I have an endpoint (Not a traditional Express app but FaaS) - called "/fileUpload"
This endpoint accepts all kind of requests - GET, POST, PUT (FaaS).
When I make a POST/PUT request with a file -
testImage.PNG
in the form data, I receive the content in the request object. FaaS runtime provides the req object as a function handler's parameter.Now, when I print - request.body of the ining POST request, I get the content as show in the image above.
If I try to upload this binary content directly to S3, it leads to corruption of the image which I believe is due to the presence of additional content in the multipart response like -
Content-Disposition
...
What I would like to do is, parse the additional multipart content like the Content-Type header and multipart boundary etc, so that I get only the image binary.
I want to extract the binary content from a multipart response that I have as follows -
I do not have access to any request object (Express/NodeJS). I just have this response object which I want to parse and extract the binary part of it.
EDIT -
To make things more clear -
I have an endpoint (Not a traditional Express app but FaaS) - called "/fileUpload"
This endpoint accepts all kind of requests - GET, POST, PUT (FaaS).
When I make a POST/PUT request with a file -
testImage.PNG
in the form data, I receive the content in the request object. FaaS runtime provides the req object as a function handler's parameter.Now, when I print - request.body of the ining POST request, I get the content as show in the image above.
If I try to upload this binary content directly to S3, it leads to corruption of the image which I believe is due to the presence of additional content in the multipart response like -
Content-Disposition
...
What I would like to do is, parse the additional multipart content like the Content-Type header and multipart boundary etc, so that I get only the image binary.
Share Improve this question edited Sep 17, 2019 at 13:54 Boudhayan Dev asked Sep 17, 2019 at 11:18 Boudhayan DevBoudhayan Dev 1,0296 gold badges22 silver badges43 bronze badges 1-
This looks like a file upload of a file named
testImage.PNG
. Do you want to store the file in your server's filesystem? Place it in an array? Or what? Please edit your question to clarify your requirement. – O. Jones Commented Sep 17, 2019 at 12:05
3 Answers
Reset to default 3To extract the binary content, we need to remove/parse the header and boundary sections of it. There exists a library - parse-multipart which makes it easy to do so.
Example -
Assuming request header contains a content-type
header.
var multipart = require('parse-multipart');
// The following section is the express endpoint/ FaaS handler
{
let header = req.headers["content-type"]
let boundary = header.split(" ")[1]
boundary = header.split("=")[1]
let body = event.data;
//console.log("Boundary - "+ boundary)
let parts = multipart.Parse(body, boundary);
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
console.log(part)
var params = {
Bucket: s3bucket,
Key: part.file,
Body: part.data,
ACL: 'public-read',
Metadata: {
'Content-Type': part.type
}
};
//Upload the data to aws
const data = await s3.putObject(params).promise()
}
return parts
}
I struggled for many days with this problem. The solution was in the API GATE WAY > configuration > Binary types add multipart/form-data
.
Observation on parse-multipart library: Be warned, this does not provide the 'name' directive of the multi-part body.
The documentation is very sketchy, reporting of error, really is not there. Apparently valid multi-part body is not parsed and the reason would not be made clear by the library.
For more pliant processing, the libraries listed in the following URL could be considered:
https://www.npmjs./package/body-parser
本文标签: javascriptHow to extractparse the binary content from a Multipart response in NodeJSStack Overflow
版权声明:本文标题:javascript - How to extractparse the binary content from a Multipart response in NodeJS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741269127a2368959.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论