Developer forum

Forum » Templates » Convert image to Base64 in a template

Convert image to Base64 in a template

Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi

I am trying to convert images to base64 as part of data we deliver from one of our solutions, but I get an access denied error

System.UnauthorizedAccessException: Access to the path 'E:\Dynamicweb.net\Application(9.10.7)' is denied.
 

Is it not possible to convert image to base64 on a dynamicweb solution?

My code is like this.

@using System.Web; 
@using
 System.IO;
@
using System.Drawing; 
 

@foreach(LoopItem i in GetLoop("ItemPublisher:Items.List")){

string tempimg=@i.GetString("ItemPublisher:Item.Image");
string DefaultImagePath = HttpContext.Current.Server.MapPath(@tempimg); 
byte[] imageArray = System.IO.File.ReadAllBytes(DefaultImagePath);  
string base64ImageRepresentation = Convert.ToBase64String(imageArray);

}

 

/Hans
 


Replies

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

What is the value of tempimg and DefaultImagePath? Try to add a check for null/empty and whether the file exists - before you call ReadAllBytes.
If thats not the problem then check windows permissions on the files folder.

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Thank you Morten. Will try that 👍🏻

/Hans

 

You must be logged in to post in the forum