Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 将文件从React应用程序传递到节点应用程序以上载到S3,在定义上载参数时是否未定义文件?_Reactjs_Amazon Web Services_Express - Fatal编程技术网

Reactjs 将文件从React应用程序传递到节点应用程序以上载到S3,在定义上载参数时是否未定义文件?

Reactjs 将文件从React应用程序传递到节点应用程序以上载到S3,在定义上载参数时是否未定义文件?,reactjs,amazon-web-services,express,Reactjs,Amazon Web Services,Express,我正在尝试使用React和Node制作自己的个人简历,几个小时以来,我一直在上传到AWS S3时遇到问题 我在提交带有文件的表单时触发事件: onSubmitChange = () => { const data = new FormData() data.append('url', this.state.url); data.append('name', this.state.name); data.append('description', this.s

我正在尝试使用React和Node制作自己的个人简历,几个小时以来,我一直在上传到AWS S3时遇到问题

我在提交带有文件的表单时触发事件:

onSubmitChange = () => {
    const data = new FormData()
    data.append('url', this.state.url);
    data.append('name', this.state.name);
    data.append('description', this.state.description);
    data.append('filePath', this.state.file);

    fetch('http://localhost:3001/new-experience', {
        method: 'post',
        headers: {'Content-Type': 'multipart/form-data'},
        body: data
        // body: JSON.stringify({
        //     url: this.state.url,
        //     name: this.state.name,
        //     description: this.state.description,
        //     filePath: this.state.file,
        // })
    })
    .then(resp => console.log(resp));
}
这就触发了这个

AWS.config.update({
    accessKeyId: process.env.AWSAccessKeyId,
    secretAccessKey: process.env.AWSSecretKey,
    region: 'eu-west-3'
});

const s3 = new AWS.S3();
app.post('/new-experience', (req, res) => {
    const { url, name, description, filePath } = req.body;

    console.log(filePath);

    const params = {
        Bucket: 'bucket-xxxx',
        Body : fs.createReadStream(filePath),
        Key : "folder/test.png"
     };

    s3.upload(params, function (err, data) {
        //handle error
        if (err) {
          console.log("Error", err);
         }

        //success
        if (data) {
          console.log("Uploaded in:", data.Location);
        }
    });

    db('mael-landrin').insert({
        experienceurl: url,
        experiencename: name,
        experiencedescription: description
    })
    .into('experiences')
    .then(resp => res.status(200).send('Experience added'))
    .catch(err => res.status(400).send(err));
})
“/new experience”路由第3行中的控制台日志返回undefined,我相信这就是为什么fs.createReadSream(filePath)将undefined赋予函数的原因,这也是我得到错误的原因

React部分中的最后一个console.log()为我提供了所有正确的选项,3个字符串和一个文件(一个.png文件)


有人知道它来自哪里吗?

文件对象不会出现在您的req.body上。您需要为文件上传安装第三方模块(当然有“原始”方式,但我不知道如何安装,也看不出有什么理由这样做):

在express根文件中:

const fileUpload = require('express-fileupload');
app.use(fileUpload());
然后在文件上载路径中:

const { url, name, description} = req.body;
const fileObject = req.files.filePath;//"filePath" is misleading, but this is the name you chose.  
const fileData = fileObject.data;

const params = {
        Bucket: 'bucket-xxxx',
        Body : fileData,
        Key : "folder/test.png"
     }

您可能想做的是将文件附加到数据,而不是文件路径。这样做吧

data.append('file', this.state.file);
您还需要一个中间件来解析多部分表单数据

穆特的工作真的很好

const { url, name, description} = req.body;
const fileObject = req.files.filePath;//"filePath" is misleading, but this is the name you chose.  
const fileData = fileObject.data;

const params = {
        Bucket: 'bucket-xxxx',
        Body : fileData,
        Key : "folder/test.png"
     }
data.append('file', this.state.file);