问题
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