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

1

0

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!

Far answered 9/6, 2015 at 16:49 Comment(0)
S
0

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.

Soutane answered 5/8, 2015 at 13:36 Comment(1)
Please review my updated question. Even while sending an instance of File, I recieve nothing on my backend.Far

© 2022 - 2024 — McMap. All rights reserved.