Sending files over ajax (superagent) to a PHP backend (Laravel)

843 views Asked by At

Recently I have been messing around with superagent in a project of mine and got to a road block. I am trying to send files via ajax to my Laravel PHP backend but I can't seem to receive anything on the backend side. I have been using superagents 'attach' method with no success.

Javascript (ES6)

createProject(input) {

    Request.post(domain + '/projects')
        .withCredentials()
        .field('project', input.project)
          // Truncated for brevity
        .attach('image', input.image)
        .end(function (err, res) {
          // Do something

        }.bind(this));
}

When I check the PHP backends received data I get an array of everything excluding the posted file.

Any help is appreciated!

1

There are 1 answers

1
Codrin Iftimie On

You can send a file via superagent using it's send method.

createProject(input) {

  Request.post(domain + '/projects')
    .withCredentials()
    .query({'project': input.project})
    .send(input.file)
    .end(function (err, res) {
      // Do something

  }.bind(this));
}

Please note that input.file is a instance of File.