1

我正在开发一个 Netlify 函数,我们在该函数中为工作申请(包括文件上传)获取表单数据,并将数据传递给第三方 API 以在他们的系统中使用。我正在关注这个方便的帖子(谢谢!)-</p>

https://www.netlify.com/blog/2021/07/29/how-to-process-multipart-form-data-with-a-netlify-function/

— 但似乎遇到了文件中的数据处理不当的情况(例如,PDF 出现空白内容,尽管 ASCII 元数据似乎至少部分完好无损),至少在使用 Netlify CLI 时是这样;我还没有尝试部署预览。写入本地目录可确认问题不在于第三方 API。处理这些文件时有什么遗漏吗?下面的示例代码(请注意,我也尝试使用缓冲区数据,结果相同)。

Fetch 函数调用 Netlify 函数:

const data = new FormData(form);

fetch('/.netlify/functions/apply', {
  method: 'POST',
  body: data, 
}).then(res => {
  if (!res.ok && res.status !== 406) {
    throw new Error('oh no');
  }

  return res.json();
}).then(data => {
  if (Array.isArray(data.missingRequiredFields) && data.missingRequiredFields.length > 0) {
    console.log(data);
    showMissingFields(data.missingRequiredFields);
  } else {
    showConfirmationMessage(data.message);
  }
}).catch(err => {
  showWarningMessage('Something went wrong; please try again.');
}).finally(() => {
  submitButton.removeAttribute('disabled');
});

这是我们的 Netlify 函数:

const Busboy = require("busboy");
const FormData = require("form-data");
const fetch = require("node-fetch");

function parseMultipartForm(event) {
  // source: https://www.netlify.com/blog/2021/07/29/how-to-process-multipart-form-data-with-a-netlify-function/
  return new Promise(resolve => {
    const fields = {};

    const busboy = new Busboy({
      // uses request headers to extract the form boundary value
      headers: event.headers,
    });

    // before parsing anything, we need to set up some handlers.
    // whenever busboy comes across a file ...
    const f = [];
    busboy.on("file", (fieldname, filestream, filename, transferEncoding, mimeType) => {
      // ... we take a look at the file's data ...
      filestream.on("data", data => {
        fields[fieldname] = {
          filename,
          type: mimeType,
          content: data,
          transferEncoding,
        };
      });
    });

    // whenever busboy comes across a normal field ...
    busboy.on("field", (fieldName, value) => {
      // ... we write its value into `fields`.
      fields[fieldName] = value;
    });

    // once busboy is finished, we resolve the promise with the resulted fields.
    busboy.on("finish", () => {
      resolve(fields);
    });

    // now that all handlers are set up, we can finally start processing our request!
    busboy.write(event.body);
  });
}
/** ***************************************************************************
 * Serverless function
 **************************************************************************** */
exports.handler = async function(event, context) {
  // parse the incoming multipart/form-data data into fields object
  const fields = await parseMultipartForm(event);

  // create new formdata object to be send to Lever
  const form = new FormData();

  for (const [key, value] of Object.entries(fields)) {
    if (key === "resume") {
      // append "resume" with the file buffer and add the file name
      form.append("resume", value.content, { filename: value.filename });
    } else {
      form.append(key, value);
    }
  }
};

您可能获得的任何指导将不胜感激。

4

0 回答 0