3

I am having some problems uploading my image from a form to my s3 bucket. At present part of the image gets uploaded, so for example 19kb instead of the full 272kb, if i try and open the image from within my bucket it's broken

app.post('/admin/addClub', (req, res) => {
  if (!req.user) {
  res.redirect('/admin');
  return;
}

// Upload image to S3
var s3Bucket = new AWS.S3( { params: {Bucket: process.env.AWS_BUCKET, Key: process.env.AWS_ACCESS_KEY_ID} } )
var data = { Key: req.body.imageBanner, // file from form
             Body: req.body.imageBanner, // Not sure here
             ACL: "public-read",
             ContentType: helper.getContentTypeByFile(req.body.imageBanner)
           };
s3Bucket.putObject(data, function(err, data){
  if (err) 
  { console.log('Error uploading data: ', data); 
    res.redirect('/admin/main');
  } else {
    console.log('succesfully uploaded the image!');
    res.redirect('/admin/main');
  }
});

Can anybody advise what i need to pass through for the Body key? as i think this must be my issue

Thanks

1
  • This is a bit off-topic, but that res.redirect('/admin/main'); could be outside the if. Commented Feb 15, 2017 at 12:34

1 Answer 1

3

You need to integrate the express-fileupload package that allows you to receive file uploads on Express.

To install run: npm install --save express-fileupload

Then you'll need to pass req.files.imageBanner.data (supposing your file upload field looks like <input name="imageBanner" type="file" />) as the Body parameter. Here's how it should look:

var fileUpload = require('express-fileupload');

app.use(fileUpload());

app.post('/admin/addClub', (req, res) => {
  if (!req.user) {
    res.redirect('/admin');
    return;
  }

  // Upload image to S3
  var s3Bucket = new AWS.S3( { params: {Bucket: process.env.AWS_BUCKET, Key: process.env.AWS_ACCESS_KEY_ID} } )
  var data = { Key: req.body.imageBanner, // file from form
               Body: req.files.imageBanner.data,
               ACL: "public-read",
               ContentType: helper.getContentTypeByFile(req.body.imageBanner)
             };
  s3Bucket.putObject(data, function(err, data){
    if (err) 
    { console.log('Error uploading data: ', data); 
      res.redirect('/admin/main');
    } else {
      console.log('succesfully uploaded the image!');
      res.redirect('/admin/main');
    }
  });
});

You can find the documentation for the express-fileupload package here:

https://www.npmjs.com/package/express-fileupload

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, I'll take a look at the package
so i seem to be getting Cannot read property 'imageBanner' of null , imageBanner being <input name="imageBanner" type="file" />
You need to require and use the express-fileupload component, as per the documentation. I've updated my answer to reflect that (it's near the top of the code I shared).
yes, I am doing that but still get the error, any further suggestions ?
ok so i've added enctype="multipart/form-data" to my form...seems to have helped

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.