0

I am struggling to find a way to convert a base 64 string to a jpeg or a file without using the Image class. Is it possible to create the file and not save it locally and upload to Azure blob storage?

var bytes = Convert.FromBase64String(base64String);
TylerH
  • 20,799
  • 66
  • 75
  • 101
Jay Jay
  • 57
  • 1
  • 11
  • 1
    What does "create the file and not save it locally" mean? "Pass it through" to where? Are you asking how to write the bytes to a file? – 15ee8f99-57ff-4f92-890c-b56153 May 24 '18 at 19:16
  • https://stackoverflow.com/a/5400225/713789 – Anirudha Gupta May 24 '18 at 19:18
  • Seems like you could use parts from [this question and answer](https://stackoverflow.com/questions/5400173/converting-a-base-64-string-to-an-image-and-saving-it). I know you are using .NET Core, so if you want a great image library thats compatible take a look at [ImageSharp](https://sixlabors.com/projects/imagesharp/) – maccettura May 24 '18 at 19:22
  • @EdPlunkett Yes, sorry. How do i pass it to a file so i can upload that file to blob storage. – Jay Jay May 24 '18 at 19:26
  • @JayJay What does "pass it to a file" mean? Describe what you want to do using common terminology. You want to create a file but you don't want to save the data to a file. You need to be more clear, because you are saying two things that contradict each other. – 15ee8f99-57ff-4f92-890c-b56153 May 24 '18 at 19:27
  • @EdPlunkett I need it as an IFormFile so i can upload it to my storage – Jay Jay May 24 '18 at 19:29

1 Answers1

3

To keep it completely clean and simple, use something like this:

using (var img = new MemoryStream(bytes))
{
    cloudBlockBlob.UploadFromStream(img);
}

This creates a MemoryStream that you can use to call CloudBlockBlob.UploadFromStream().

Edit
Or, like @mike-z said in the comment below, you can use CloudBlockBlob.UploadFromByteArray() directly.

rickvdbosch
  • 14,105
  • 2
  • 40
  • 53