X
X
xxx1233212018-10-13 16:19:36
JavaScript
xxx123321, 2018-10-13 16:19:36

How to accept an image on the server?

On the client I do:

let button = document.getElementById('btn')
button.onclick = () => {
    var formData = new FormData();
    formData.append("zdarov", document.getElementById("file").files[0], 'a.png');
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "http://localhost:3000/api");
    xhr.send(formData);
}

And the server receives an empty object:
router.post('/api', ctx => {
    ctx.status = 200
    console.log(ctx.request.body)
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2018-10-13
@xxx123321

https://www.npmjs.com/package/koa-multer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question