Hi all,
I’m developing an application for the marketplace and facing an issue with the files API.
Except for .png files, all the files I’m trying to upload, contain a ‘null’ extension and as a result, the file cannot be previewed.
I’m using the code below in Node.js:
const upfile = req.file.path;
// set auth token and query
const API_KEY = req.owner;
let query = `mutation ($file: File!) { add_file_to_column (file: $file, item_id: ${parseInt(
req.body.itemId
)}, column_id: "${req.body.columnId}") { id } }`;
const url = 'https://api.monday.com/v2/file';
let boundary = 'xxxxxxxxxx';
let data = '';
fs.readFile(upfile, function (err, content) {
data += '--' + boundary + '\\r\\n';
data += 'Content-Disposition: form-data; name="query"; \\r\\n';
data += 'Content-Type:application/json\\r\\n\\r\\n';
data += '\\r\\n' + query + '\\r\\n';
data += '--' + boundary + '\\r\\n';
data +=
'Content-Disposition: form-data; name="variables"file]"; filename="' +
req.file.originalname +
'"\\r\\n';
data += 'Content-Type:application/octet-stream\\r\\n\\r\\n';
let payload = Buffer.concat(e
Buffer.from(data, 'utf8'),
new Buffer.from(content, 'binary'),
Buffer.from('\\r\\n--' + boundary + '--\\r\\n', 'utf8'),
]);
const options = {
method: 'post',
headers: {
'Content-Type': 'multipart/form-data; boundary=' + boundary,
Authorization: API_KEY,
},
body: payload,
};
fetch(url, options)
.then((res) => res.json())
.then((json) => {
fs.unlink(upfile, function (err) {
if (err) throw err;
console.log('File deleted');
});
}).catch((err) => {
console.log(err);
});
});
It’s important to mention that the req.file.originalname
contains the name as well as the extension of the file, does someone know what can cause such a problem?
Thanks.