3

Note: I am currently working on the api part of the app and I have no access to the frontend stuff.

So the frontend will send me a base64 string that I need to convert to a FormFile because the method that would upload files in s3 will only accept IFormFileCollection so I will need to convert the base64 string to a Formile and also it's too complicated to just refactor the whole upload method now.

Is there a way to do this?

Thanks!

Zyx Sun
  • 409
  • 4
  • 17
  • 2
    Possible duplicate https://stackoverflow.com/questions/25919387/converting-file-into-base64string-and-back-again – KJSR Aug 12 '19 at 08:36
  • 1
    You can use a memory stream so you do not need to read/write a file. – jdweng Aug 12 '19 at 09:03
  • 1
    _"because the method that would upload files in s3 will only accept IFormFileCollection"_ - so, create an overload that also accepts a stream? – CodeCaster Aug 12 '19 at 09:07
  • The S3 .NET Client SDK doesnt' work with IFormFile. It works with streams or file paths. The method that accepts `IFormFileCollection` will have to get their contents as a stream one way or another. Most likely, all you have to do is *bypass* the method that expects IFormFile objects and call the methods that expect a stream – Panagiotis Kanavos Aug 12 '19 at 09:23

1 Answers1

2

something like

File.WriteAllBytes(@"c:\yourfile", Convert.FromBase64String(yourBase64String));

using (var stream = File.OpenRead(@"c:\yourfile"))
{
    var formFileName = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name))
    {
        Headers = new HeaderDictionary(),
        ContentType = "YOUR CONTENT TYPE HERE"
    };
}
Idriss Benbassou
  • 1,481
  • 2
  • 14
  • 23