-3

I am new to C# and I'm really having a trouble on finding a way to convert files(doc, xlsx, jpg, png, etc.) to base64.

So far, what I got is a way to retrieve the path using Directory.GetFiles()....but this is not the result I was expecting.

What I expected it to do is get the data of the files(not the path) and convert it to base64 so that I can display it on my front-end.

Any idea?

Always_a_learner
  • 1,254
  • 1
  • 8
  • 16
aznmik
  • 45
  • 1
  • 9
  • 4
    Please google a bit more and start searcing on stackoverflow and if you have a specific problem you can post that here. – Richárd Baldauf May 27 '20 at 07:58
  • https://stackoverflow.com/questions/25919387/converting-file-into-base64string-and-back-again – Nafis Islam May 27 '20 at 07:59
  • Check this: https://stackoverflow.com/questions/13301053/directory-getfiles-of-certain-extension – JaredKarl May 27 '20 at 08:00
  • Does this answer your question? [Converting file into Base64String and back again](https://stackoverflow.com/questions/25919387/converting-file-into-base64string-and-back-again) – Always_a_learner May 27 '20 at 08:02

1 Answers1

1

Try this:

foreach (string filePath in  Directory.GetFiles(@"C:\directory"))
        {
            Byte[] bytes = File.ReadAllBytes(filePath);
            String file = Convert.ToBase64String(bytes);
        }
Always_a_learner
  • 1,254
  • 1
  • 8
  • 16