Загрузить файл в Node.js из React - PullRequest
1 голос
/ 01 мая 2020

Я хочу загрузить файл в node.js, но с определенным путем c, я использую multer в node.js, но у меня нет идеи, чтобы отправить путь от реакции, код mi:

state = {
      selectedFile: null,
        }

fileSelectedHandler = event => {
        this.setState({
            selectedFile: event.target.files[0]
        });
    }
fileUploadHandler = () => {
        const fd = new FormData();
        const config = {
            headers: {
                'content-type': 'multipart/form-data'
            }
        };
            fd.append('noren', this.state.selectedFile)
            axios.post('uploadFile', fd, config)
                .then((response) => {
                    alert("El archivo se subio exitosamente");
                }).catch((error) => {
            });
    }

и код в узле следующий:

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, '/var/pruebas/'+type);
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname)
    }
})

var upload = multer({storage: storage}).single('noren')

uploadContr.uploadFile = upload, (req, res, next) => {
    console.log('La ruta es: ', req.path)
    const file = req.file
    if (!file) {
        const error = new Error('Please upload a file')
        error.httpStatusCode = 400
        return next(error)
    }
    return res.send({status: 'ok'})
};

Я пытаюсь отправить путь в качестве параметра в топоре ios со следующей формой:

fd.append('noren', this.state.selectedFile)
            axios.post('uploadFile', fd,{data:{path: this.props.path}} config)
                .then((response) => {
                    alert("El archivo se subio exitosamente");
                }).catch((error) => {
            });

или

fd.append('noren', this.state.selectedFile,this.props.path)
            axios.post('uploadFile', fd, config)
                .then((response) => {
                    alert("El archivo se subio exitosamente");
                }).catch((error) => {
            });

но путь не отправлен. Я надеюсь, что может помочь мне

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...