//Function to create and attach a text file
function attachTextFileToKintone() {
var appId = kintone.app.getId(); // Replace with your Kintone App ID
var recordId = kintone.app.record.getId(); // Replace with the record ID to attach the file
var fileFieldCode = ‘TOTFILE’; // Replace with your Kintone File Field Code
var apiToken = ’ — ';
var text = “This is a sample text file.”;
var fileName = “sample.txt”;
var blob = new Blob([text], { type: ‘text/plain’ });
var formData = new FormData();
formData.append(fileFieldCode, blob, fileName);
var xhr = new XMLHttpRequest();
xhr.open(‘POST’, ‘https:// — .cybozu.com/k/v1/record/attachment.json’, true);
xhr.setRequestHeader(‘X-Requested-With’, ‘XMLHttpRequest’);
xhr.setRequestHeader(‘Authorization’, ‘Bearer’ + apiToken); //Uncomment this line if authentication is required
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
alert(‘Text file attached successfully!’);
console.log(xhr.responseText);
} else {
alert('Error attaching text file: ’ + xhr.status);
console.error(xhr.responseText);
}
}
};
xhr.send(formData);
}
// Create a button
var button = document.createElement(‘button’);
button.innerHTML = ‘Attach Text File’;
button.addEventListener(‘click’, attachTextFileToKintone);
// Append the button to the page
document.body.appendChild(button);
});
})();