I'm trying to write codes with javascripts to upload a file version with the endpoint:
Question
How to upload a file with axios request which can run both in the nodejs and browser env
It works well if I pass the File object in the formData for the file parameter.
But as the File type only exists in the browser, so I tried to use arraybuffer/buffer as the file value(I also don't want to use the fs.stream as it only works in the nodejs), but it seems the result is not correct after I have uploaded the content to the server(the file content is changed).
But if I set the content to be plain text, the upload result also works well.
So, my question is how to pass an arraybuffer/buffer/string as the content for a file?
The following are my codes:
let body = "";
const binary = this.ab2str(_fileBuffer);
body +=
"--" +
boundary +
'\r\nContent-Disposition: form-data; name="attributes"' +
"\r\n\r\n" +
`{"name": "${item.filename}"}` +
"\r\n" +
"--" +
boundary +
'\r\nContent-Disposition: form-data; name="file"; filename="' +
item.filename +
'"' +
"\r\nContent-type: application/octet-stream" +
"\r\n\r\n" +
binary +
"\r\n" +
"--" +
boundary +
"--\r\n";
try {
const response = await axios.post(
`${getUploadFileVersion(identifier)}`,
body,
{
headers: {
"Content-Type": `multipart/form-data; boundary=${boundary}`,
Authorization: `Bearer ${tokenInfo.accessToken}`
}
}
);
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.
