gpt4 book ai didi

node.js - 使用 react-dropzone-uploader 获取服务器响应

转载 作者:行者123 更新时间:2023-12-04 14:16:18 25 4
gpt4 key购买 nike

如何使用 react-dropzone-uploader 获得服务器响应.我一直在使用它,我试图找到一种在文件上传或失败后从 nodejs 后端接收响应到 react 前端的方法。

前端( react ):

const getUploadParams = (file) => {

return {url: uploadUrl, headers: {"Authorization": `Bearer ${token}`}}
}

const handleChangeStatus = ({ meta, remove }, status) => {
if (status === 'done') {
notify(`${meta.name} uploaded!`)
remove()
} else if (status === 'aborted') {
notify_error(`${meta.name}, Upload failed...`)
}
}

后端上传到谷歌云存储,如果成功,我想向前端发送一个响应,以便能够通知用户文件上传的状态

后端(nodejs):

bucket.upload(file.path, {
public: true,
destination: set_user_folder + file.name,
resumable: true,
}, function (err, file, apiResponse) {
if (err) {
return res.status(200).send({
message: err,
type: "error"
});
} else {
console.log(apiResponse);
return res.status(200).send({
message: "File Uploaded",
type: "success"
});
}
});

最佳答案

您可以像这样从 handleChangeStatus 获取服务器响应:

const handleChangeStatus = ({ meta, file, xhr}, status) => {
if (status === 'done'){
let response = JSON.parse(xhr.response);
}
};

关于node.js - 使用 react-dropzone-uploader 获取服务器响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59946551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com