"use strict";
const stream = require("stream");
const Busboy = require("busboy");
const handlePost = (event) => {
return new Promise((resolve, reject) => {
const busboy = new Busboy({ headers: event.headers });
let html = "";
busboy.on("file", (fieldname, file, filename, encoding, mimetype) => {
let buf = Buffer.alloc(0);
console.log({ fieldname });
file.on("data", function (data) {
buf = Buffer.concat([buf, data]);
});
file.on("end", function () {
const imgBase64 = buf.toString("base64");
html += `<img src="data:${mimetype};base64, ${imgBase64}" />`;
});
});
busboy.on("finish", function () {
console.log({ msg: "Parse form complete!", html });
resolve({
statusCode: 200,
headers: {
"content-type": "text/html",
},
body: html,
});
});
const bodyBuf = Buffer.from(event.body, "base64");
var bufferStream = new stream.PassThrough();
bufferStream.end(bodyBuf);
bufferStream.pipe(busboy);
});
};
const handleGet = (event) => {
const html = `<html><head></head><body>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="image-1" accept="image/*"><br />
<input type="file" name="image-2" accept="image/*"><br />
<input type="submit">
</form>
</body></html>`;
console.log({ msg: "Get form complete!", html });
return {
statusCode: 200,
headers: {
"content-type": "text/html",
},
body: html,
};
};
exports.main_handler = async (event, context) => {
const method = event.httpMethod;
if (method === "POST") {
return handlePost(event);
}
if (method === "GET") {
return handleGet(event);
}
};
本页内容是否解决了您的问题?