Copied to clipboard

Flag this post as spam?

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


  • Quentin 2 posts 71 karma points
    Feb 08, 2024 @ 21:43
    Quentin
    0

    Convert a file from the backoffice to a byte array

    Hello,

    I am trying to access a media uploaded on the backoffice via an Umbraco API Controller (because I will call the method from my frontend with Ajax). The media is a .xlsx file that I have to convert to a byte array (I think) in order to browse it and return the data I want to the client.

    Here is the start of the method :

     public async Task<IList<ProfessionnelRepertoireViewModel>> GetRepertoireDesProfessionnels()
        {
            var content = _contentQuery.ContentSingleAtXPath("//homePage/espaceAcheteur/repertoireDesProfessionnels");
            var repertoireDesProfessionnels = content.Value<MediaWithCrops<File>>("professionalsDirectory");
    
            return new List<ProfessionnelRepertoireViewModel>();
        }
    

    I successfully retrieved the file (which is repertoireDesProfessionnels) as a MediaWithCrops<File>. Now, I'm really lost on what to do next. I think I tried everything, IMediaService, MediaFileManager and then some but I can't wrap my head around what I should now.

    Thanks for your help!

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Feb 09, 2024 @ 09:12
    Marc Goodson
    0

    Hi Quentin

    Wondering if this discussion on this closed issue would give you a steer?

    https://github.com/umbraco/Umbraco-CMS/issues/11539

    Here they are talking about a 'tag helper' but the input to the tag helper is a 'relative Url', which you'd be able to get from your prepertoireDesProfessionanels.Url()...

    The gist seems to be to locate the 'path' of where the media item is actually stored, and Umbraco has an IFileSystem implementation that helps you do that based on the media file system provider your site is using, eg on disk or cloud...

    looks like once you have that path you can use the MediaFileManager.FileSystem to open the file as a stream...

    ... and then I think dot net core has a special 'File' action result that you can use to return a FileStreamResult

    return File(yourStream, "application/octet-stream", "{{yourfilename.xslx}}"); // returns a FileStreamResult

    but it's a long time since I've done something like this!

    regards

    marc

  • Chris Speakman 17 posts 151 karma points
    Feb 22, 2024 @ 17:58
    Chris Speakman
    0

    Hi Quentin,

    Don't know if you solved this already but I use the following method in some of my projects to get a byte array of an image file from its URL - should work just as well for your xlsx file.

    private byte[] DownloadImage(Uri uri)
    {
        using var httpClient = new HttpClient();
    
        // Download the image
        var task = Task.Run(async () => await httpClient.GetByteArrayAsync(uri));
        task.Wait();
        var imageBytes = task.Result;
    
        return imageBytes;
    }
    

    If needed I use a helper method to get the file name from the URL:

    private string GetFileNameFromUrl(string url)
    {
        var decoded = HttpUtility.UrlDecode(url);
    
        if (decoded.IndexOf("?") is { } queryIndex && queryIndex != -1)
        {
            decoded = decoded.Substring(0, queryIndex);
        }
    
        return Path.GetFileName(decoded);
    }
    

    And finally, to get the full URL (including domain) from your MediaWithCrops object you should be able to use:

    mediaWithCrops.Url(mode: UrlMode.Absolute)
    

    Hope that helps!

Please Sign in or register to post replies

Write your reply to:

Draft