0

With this code I am able to convert base64 to an image.

What I am trying to do instead is convert to other files like pdf, json, xml etc. from base64.

[HttpPost("image")]
public async Task<IActionResult> UploadImage(FilesDetails imageDetails)
{ 
  if (imageDetails == null)
    return BadRequest();

  byte[] bytes = Convert.FromBase64String(imageDetails.Image);

  Image image;
  using (MemoryStream ms = new MemoryStream(bytes))
  {
    image = Image.FromStream(ms);
  }
  image.Save(imageDetails.Name + '.' + imageDetails.Type, ImageFormat.Png);
  return Ok(image);
}
burnsi
  • 6,194
  • 13
  • 17
  • 27
RoseG
  • 1
  • 1
  • if i understood you correctly then you want to have similar methods for other file types. right ? – CodingMytra Jun 23 '22 at 09:23
  • 1
    You can just write the byte[] data to a binary file. Even for an image the step with the image class is not necessary, if you just want to store it to a file. – jps Jun 23 '22 at 09:30
  • @Nitz yes i need similar methods .if possible also get file details from base64 string like whether it is pdf,txt,jpg etc.. – RoseG Jun 23 '22 at 10:11
  • can you update the structure your FilesDetails in your question. – CodingMytra Jun 23 '22 at 10:14
  • @Nitz , in my present code if I encode an image with base64 and pass that string in this code it will perfectly work. But, now what I am trying to do is convert any base64 string , either of pdf or text or image etc, and the result i want while decoding the string i should get its file type(whether it of png,jpg,pdf),and the original file and iIhave save that orginial(if the original file is pdf then i should get a pdf after decoding) file in a folder. – RoseG Jun 23 '22 at 10:42
  • I suggest you can check the file extension by base64 string or byte array. Reference: https://stackoverflow.com/a/41219153/11398810, https://stackoverflow.com/a/13614746/11398810. – Rena Jun 24 '22 at 06:59
  • @CodingMytra structure?? – RoseG Jul 07 '22 at 10:16
  • I meant the definition of "FilesDetails" – CodingMytra Jul 07 '22 at 11:41
  • what is modalname ? – CodingMytra Jul 07 '22 at 17:38
  • @RoseG you are not helping with these comments. what i meant to ask was to provide class definition of "FileDetails" what you are passing in Post method. what all properties or fields does it contains . – CodingMytra Jul 08 '22 at 06:59
  • @CodingMytra sorry my modal contains id(int),name,date,file name ,file details – RoseG Jul 08 '22 at 09:00

0 Answers0