node.js - 使用 react-dropzone-uploader 获取服务器响应
问题描述
如何使用react-dropzone-uploader获得服务器响应。我一直在使用它,我试图找到一种在文件上传或失败后从 nodejs 后端接收响应到反应前端的方法。
前端(反应):
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);
}
};
推荐阅读
- html - 角 - *ngFor
- windows - 在 Windows 10 上的 python 中安装 tkinter 时出错
- java - SpringBoot OAuth2 自定义异常响应
- pkcs#11 - 带有 PKCS#11 的 AWS cloudhsm 无法导出 RSA 公钥
- mongodb - Next JS 与 Apollo 和 MongoDB 的连接
- android - 从以前的 Fragment 中的 Firebase 数据库中获取数据
- jenkins - Jenkins 自动发现我的 github 存储库
- django - 米学习..如何以 jinja 格式签入 html 文件?它给了我错误无法解析剩余部分:来自'x.price>4000'的'>4000'
- ios - 折线未在swiftUI中显示
- javascript - 带有嵌套子数组的JS递归函数