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.
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!
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:
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 :
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!
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
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.
If needed I use a helper method to get the file name from the URL:
And finally, to get the full URL (including domain) from your MediaWithCrops object you should be able to use:
Hope that helps!
is working on a reply...