How to post binary using Axios?

Che*_*enL 6 javascript post axios

I'm trying to make a post request to a server that accepts a binary file upload with Authentication token in header

I was able to achieve this using XMLHttpRequest(), but are there ways to use axios to achieve the same thing?

I've tried

axios.post(url, File, {
    headers: {
        'Content-Type': File.type,
        'Authentication' : faketoken
    }
})
Run Code Online (Sandbox Code Playgroud)

where File is an instance of Html5 File interface, this doesn't work, for some reason when I exam the request header in chrome, the content-type is application/x-www-form-urlencoded

thanks in advance

Regards

Mur*_*ran 3

您可以将文件上传到接受二进制文件上传的 API,如下所示:

const file = fs.readFileSync("/path/to/file");
await axios({
    method: 'post',
    url: uploadUrl, //API url
    data: file, // Buffer
    maxContentLength: Infinity,
    maxBodyLength: Infinity
});
Run Code Online (Sandbox Code Playgroud)