Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

javascript - how to send zip file as body in a post request in react?

Let's say I have a zip file uploaded by user, how do I pass it to the server for process?

I have front end function to handle zip file. I can verify that fileUploaded holds the zip file.

  const onImport = async evt => {
    const fileUploaded = evt.target.files[0];
    const response = await extractImport('/extractimport', fileUploaded);
  };

Helper function used above

export const extractImport = async (url, fileUploaded) => {
  return await fetch(url, {
    method: 'POST',
    headers: {'accept': 'application/zip'},
    body: fileUploaded
  });
};

router for post call. And here, req body is empty. I don't know what went wrong here.

router.post('/extractimport',
  asyncWrapper(async (req, res) => {
    //req.body is empty here, I dont understand why.
    try {
      const result = await extractImportPost(req.body);
      res.status(200).json(result).end();
    } catch (err) {
      res.status(500).json({errors: [{error: err.message}]}).end();
    }
  })
);

extractImportPost utility used above is as below

const extractImportPost= async (zipFile) => {
  // zipFile is undefined here.
  // how do I pass the zip file to this place for process
}

Thank you for your time.

question from:https://stackoverflow.com/questions/65914352/how-to-send-zip-file-as-body-in-a-post-request-in-react

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...