0

I have a csv file which contains latin characters (Ascii value > 127). The file gets uploaded with any type of encoding and shows the right data after uploading. But it gets converted automatically to UTF8 after performing operations on the file.

But I am not able to see the same characters when it is converted to UTF8 after performing operations.

I believe if I will upload the files with UTF8 encoding only then I will see the same characters that were present while uploading the file. So I want to encode the file with UTF8 Encoding.

I am getting IForm File from the function. I tried these methods to change the encoding but it does not affect the file in any way.

First method

//'file' is the IForm file 
string[] filecontent;

StreamReader sr = new StreamReader(file.FileName);
string data = sr.ReadLine();
filecontent = data.Split(",");
File.WriteAllLines(file.FileName, filecontent, Encoding.UTF8);

Second method

var fileStream2 = File.OpenWrite(file.FileName);
var sw = new StreamWriter(fileStream2, Encoding.UTF8, 1024, false);
sw.Write(fileStream2);
sw.Close();

Is there any other method to do this or is there any other library to encode the csv file with UTF 8 directly?

  • The first method *appears* to turn the columns of the first row (only) into lines; it should be changing the file *hugely* - however, you don't seem to specify a particular encoding when *reading* the file (`StreamReader`). If there is an encoding mismatch, both read and write need to be very explicit. You would also need to *know* what encoding/code-page it was originally written with. Do you know the original encoding/code-page? If not: you're kinda doomed - you can only guess, and guessing hasn't worked well so far. – Marc Gravell Oct 13 '21 at 08:42
  • also: "and shows the right data after uploading" - *where* does it show the right data? where are you looking to see this? – Marc Gravell Oct 13 '21 at 08:46
  • In the above methods, I was trying to recreate the file or make changes to the file. But I want to directly convert the csv file (Any encoding) to csv(Utf-8). That's the main question. Is it possible to do that? or is there any library to do that? @MarcGravell – Shishank Jain Oct 14 '21 at 08:05
  • if you don't know the encoding, then **you cannot reliably read the contents**; so, once again, *do you know the encoding*? If you don't: you can't do this, because you will absolutely fail at the first step. When dealing with code-pages, knowing the specific code-page is *necessary* and cannot be omitted. Once you can *read* the contents reliably, writing them as UTF-8 is trivial. – Marc Gravell Oct 14 '21 at 08:08
  • Okay Thanks for clearing out the problem. @MarcGravell – Shishank Jain Oct 14 '21 at 08:10

0 Answers0