Using the DBF library, I am trying to create a converter from a CSV file to DBF. The conversion should take place on the browser side. I have a problem – the Cyrillic characters are not displayed correctly in the created dbf file. I tried many options, but nothing worked.
csv file can have different encodings
const [csvData, setCsvData] = useState(null);
const handleFileUpload = (event) => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
const text = e.target.result;
const result = parse(text, { header: true });
setCsvData(result.data);
};
reader.readAsText(file);
}
};
const convertToDBF = () => {
if (csvData) {
try {
const fields = Object.keys(csvData[0]).map(key => ({
name: key,
type: 'C',
size: 10
}));
const records = csvData.map(row => {
const record = {};
Object.keys(row).forEach(key => {
record[key] = row[key] ? row[key].toString() : '';
});
for (const key in record) {
if (typeof record[key] === 'string') {
record[key] = iconv.encode(record[key], 'UTF-8');
} }
return record;
});
const dbfBuffer = structure(records, fields);
const blob = new Blob([dbfBuffer], { type: 'application/octet-stream' });
saveAs(blob, 'output.dbf');
} catch (error) {
console.error('Error during DBF conversion:', error);
}
}
};
return (
<div>
<input type="file" accept=".csv" onChange={handleFileUpload} />
<button onClick={convertToDBF} disabled={!csvData}>to DBF</button>
</div>
);
};
export default App;```
I tried to change the encodings. I expect a readable display of Cyrillic characters
New contributor
Віталій Тимів is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.