C5-8.3.0 How to attach file to email?

Permalink
Hello.

I have an ajax form which works without file attachment. Now I want to add file attachment to it. I do this in a view.js:
var form_data = new FormData();
form_data.append('image', $('#image')[0].files);

and this in the controller:
$image = $_FILES['image']['tmp_name'];
$image_name = $_FILES['image']['name'];
$importer = new \Concrete\Core\File\Importer();
$image_version = $importer->import($image, $image_name);
$attachment = $image_version->getFile();
....
$mh->addAttachment($attachment);

But it throws an error at the getFile() line of the above code: Call to a member function getFile() on integer (0)

Does it mean the file is not passed to php? What am I doing wrong?

Thank you.

linuxoid
 
linuxoid replied on at Permalink Reply
linuxoid
form_data.append('image', $('#image')[0].files[0]);

The [0] at the end was the issue. All examples I've seen were sending multiple files. I only needed to send 1. So I just removed the [0] and... a few hours later...