首页 > 解决方案 > 使用 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"
                                    });
                                }
                            });

标签: node.jsreactjsgoogle-cloud-storage

解决方案


您可以像这样获得服务器响应handleChangeStatus

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

推荐阅读