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

爱⌒轻易说出口 提交于 2019-12-08 09:40:03

问题


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:


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.



来源:https://stackoverflow.com/questions/30738201/sending-files-over-ajax-superagent-to-a-php-backend-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!