Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Alexandre 4 posts 24 karma points
    Mar 07, 2014 @ 20:00
    Alexandre
    0

    Saving image as base64

    Hi,

    I'm new to Umbraco and right now I need to find a way to save the images as a base64 string. There will be no Writing permissions on File server and I'm far from finding a viable solution.

    I need to change the FileUpload component as this need to be done for every image upload available and no other file extension will ever be uploaded. A solution I found is to change the XML saved to "cmsContentXml" replacing the path to the file to "data:image/png;base64," plus the base64 code, so when its read from table it would set that as the image src, but I also couldn't find a way to do this, as I can't find any .cs file (neither in the installed version nor the Source Code I downloaded from github).

    I'm using VS2010 right now, if that matters.

    If I'm not providing enough information, please ask for what else you need to know.

    Thanks a lot in advance,

     

    Alexandre

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Mar 22, 2014 @ 20:02
    Jan Skovgaard
    0

    Hi Alexandre and welcome to our :)

    May I ask why you need/want to base64 encode your images?

    I'm just curious and don't currently have any cues on how to ahcieve this. But I'd love to know why.

    /Jan

  • Alexandre 4 posts 24 karma points
    Mar 23, 2014 @ 03:44
    Alexandre
    0

    Hey Jan, thanks for the welcome!

    I need to encode de images as they will have to be saved only on the database, the environment I'm setting up has a 'No file writing policy', so I can't upload the file to media folder and save the path reference on database. My deadline is already over, but I'm still working on it whenever I have time, and I think I'm pretty close to it, lets see how it goes!

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Mar 23, 2014 @ 10:07
    Jan Skovgaard
    0

    Hi Alexandre

    Ok, that sounds interesting. But how about other media like pdf files, word documents etc.?

    It would be ace if you would blog about it once you're done. Always nice to know about :)

    /Jan

  • Alexandre 4 posts 24 karma points
    Mar 23, 2014 @ 13:21
    Alexandre
    0

    I did ask about other file extensions when those specs were given and the answer was that no other file type will ever be uploaded. I do agree with you its a risk not worth taking, but I couldn't find another solution up to now.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Mar 23, 2014 @ 14:52
    Jan Skovgaard
    0

    Hi Alexandre

    If you want to limit the risk of any editors making any error in regards to uploading other media then images you could perhaps try to do these 2 thing

    1: Remove the option to manually create a file in a folder by going to the settings section and find the folder type in the media folder and remove the checkmark from file in the structure tab

    2: In the /config/umbracoSettings.config it's possible to setup file types that should be ignored if you go to the

    Hope you find this usefull.

    /Jan

  • Alexandre 4 posts 24 karma points
    Mar 24, 2014 @ 15:48
    Alexandre
    0

    I certainly did! Thanks a lot!

    I'm trying to make it so any file can be uploaded to database. I know the storage impact there, but as only images will be stored (and limited to that in in the umbracoSettings.config), things will go as expected and if anytime they request me to allow any file type, the damage will be reduced.

    Thanks a lot!

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Mar 24, 2014 @ 18:43
    Jan Skovgaard
    0

    Hi Alexandre

    Glad I could help.

    Hope you will post / blog about how you did things.

    Cheers, Jan

  • Phil Veloso 38 posts 251 karma points
    Sep 28, 2019 @ 10:53
    Phil Veloso
    0

    If anyone stumbles across this thread, here is my solution:

        public static string Base64Image(string url, string type)
        {
            string[] types = { "remote", "local" };
    
            if (url != null || type != null && types.Contains(type))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    var webClient = new WebClient();
                    string currentURL = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
                    string getUrl = type != "local" ? url : currentURL + "/" + url;
    
                    try
                    {
                        byte[] getBytes = webClient.DownloadData(getUrl);
                        return Convert.ToBase64String(getBytes);
                    }
                    catch (WebException e)
                    {
                        Current.Logger.Info<Base64Image>("Error: {e}", e);
                        return string.Empty;
                    }
                }
            }
            return string.Empty;
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft