3

My Angular app sends a FormGroup to a Node.js server & then sends that form content via an Email.

At the moment, I can populate the email body like so:

<tr>
    <th>Town</th><td>${content['formBody']['personalInfo']['town']}</td>
</tr>

and so on...

But, my form also has a File Upload control which I want to use to add file attachments to the email also.

Here is what I have at the moment:

<td>${content['formBody']['send']['fileUpload']}</td>

But instead of the actual selected file, "object Object" is appearing in the email body currently.

Is there a way I can attach the selected files to the email using the above approach, or is there a different way? Thanks a lot

user9847788
  • 2,135
  • 5
  • 31
  • 79

1 Answers1

1

What are you using in Node.js for getting the files? A few months ago I needed to upload files and used Multer, a npm package for handling formdata files in node js. With it, you can use the files received on the backend and put it on the email.

In the frontend file

//Method to do the request
superagent
.post(/register")
.attach("avatar", uploadedImage)

uploadedImage has the image content that you get in the VueJS component

In the backend file

var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
import fs from 'fs-extra'

router.post('/register', upload.single('avatar'), (req, res, next) => {
    return fs.readFile(req.file.path)
            .then(content => {
                // The content of the file
            })
}

For more information, here you have the answer Accessing data sent as FormData using Axios

Does it help?

Pedro Silva
  • 2,655
  • 1
  • 15
  • 24
  • Currently, I'm just posting the form data to the server & adding that data to the email body as I've shown above. But I haven't done anything specific for the files. – user9847788 Apr 30 '19 at 11:22
  • 1
    Nevermind, my answer isn't correct. If you want to see if the file content is correct do JSON.stringify(content['formBody']['send']['fileUpload']) that will transform the object to string – Pedro Silva Apr 30 '19 at 11:27
  • I ran this in my Post: `var file = JSON.stringify(content['formBody']['send']['fileUpload']) console.log("File Upload - " + file);`, & the following was outputted: **File Upload - [{"file":{},"id":0,"icon":"doc","src":{"changingThisBreaksApplicationSecurity":"data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,"}}]** – user9847788 Apr 30 '19 at 11:44
  • Is this on the frontend ? – Pedro Silva Apr 30 '19 at 12:33
  • No, this is in backend, my Node.js (app.js) – user9847788 Apr 30 '19 at 12:34
  • So, you are not receiving the file correctly on your backend, the file object is empty. You should try my initial suggestion. – Pedro Silva Apr 30 '19 at 12:35