A_G
A_G

Reputation: 2370

Encoding Accented Characters in React JS

The user uploads CSV with accented characters like émily, ástha which I need to encode and pass to the backend. I have tried updating the file type in FormData between type: 'text/plain' and type: 'text/csv'.

const data = new FormData()
data.append('file', new Blob([params.file], { type: 'text/csv' }))

Passed encoding in FileReader's method readAsText with params ISO-8859-1, ISO-8859-4, ISO-10646-UCS-Basic, and a couple of other examples from http://www.iana.org/assignments/character-sets/character-sets.xhtml. Needless to say, tried other unhelpful methods readAsArrayBuffer and readAsBinaryString.

reader.readAsText(file, 'ISO-8859-4')

Printing using ISO-8859-1 gives me Žmily and ‡stha which are not close enough. I need to be able to receive the exact characters (backend uses ascii folding filter to convert these into ascii).

I am relying on How to encode & decode non Ascii characters? as the last solution.

Any pointers on how can I attempt encoding non ascii characters?

Upvotes: 0

Views: 1525

Answers (1)

Anthony Liu
Anthony Liu

Reputation: 322

This might be related with the csv encoding issue. You can prepend the Byte-Order-Mark (BOM) to csv content first, try

const BOM = "\uFEFF"; 

new Blob([BOM + params.file], { type: 'text/csv;charset=utf-8' })

Upvotes: 1

Related Questions