How to output the contents of a file in razor in u...
# help-with-umbraco
t
I have a File Upload field in which I select a JSON file. The docs here only show how to get the filename: https://docs.umbraco.com/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/file-upload But I need the editor to be able to select a JSON file and then have the contents of that file available for calculation. First step would be to just output this is raw text in my razor template, but I am struggling to see how - am I supposed to use a mediaservice or some helper method? Also, is this something that has to be done differently in Umbraco Cloud, given the nature of media file hosting?
m
Guess it depends if you are reading it in a memory stream to use at the point of upload.. or saving for future use? If you do need to save and read into the media folder.. then as per the example in the doc you've posted you can use
_mediaFileManager
to abstract away having to work out and deal with what filesystem infrastructure is in place. source here might give you pointers fo using the mediaFileManager to do your heavy lifting.. https://github.com/umbraco/Umbraco-CMS/blob/contrib/src/Umbraco.Infrastructure/PropertyEditors/FileUploadPropertyValueEditor.cs
t
Basically it's a json file that is used to configure the display of a chart view. Not sure which of the approaches you mention there would work best. The client/editor of the site expects to be able to upload different configuration files and swap this out for the content block when needed. Does that sound more aligned to either of the approaches you reckon?
m
Does it need to be a frontend upload? Or is this actually a backend cms task?
j
What are you actually doing with the JSON in the file? Deserializing into a C# object? Just printing it on the page? I ask because if it's just for some JS to use then having JS in the browser fetch the file from the media URL is a quick win with minimal performance penalty. Otherwise, with Cloud using blob storage you really don't want to be fetching that file from the filesystem on demand with each request, it's very inefficient (in fact, unless your hosting on something with a fast NVME SSD reading files for each request is almost always a bad idea). Your best bet is to store the JSON in content as a property value on save, as that will be cached, and just use it like any other property. Two simple options to achieve this - custom property editor that just takes a file input and saves the JSON to a property, or on the media save notification read out the file's content to a label on the media node.
t
Sorry I should've been clearer about the use case, it is only the editor that uploads a file in the backoffice, there is no uploads from the site visitor. Yes, Jason, that sounds like the best solution actually! The file contents should not be exposed to the browser, it is parsed, used in an API call and then the end result is shown to the client when loading page. Preferrably the API call should be done only once as well but this is "moving" data so it has to be done on demand.
What I'm really stuck at though is how to read the contents of a file, somehow. Right now I'm handling things in a MediaSavingNotification, ready to populate my custom property with the contents of the file being uploaded. `umbracoFile`'s value is the file path, so far so good. But should I just make some standard C# implementation for this or is there an Umbraco way? The MediaFileManager seems to handle uploading/creation of files rather than reading files.
j
You can use the mediaFileSystem:
Copy code
csharp
var mediaFileSystem = _mediaFileManager.FileSystem;
var fileStream = mediaFileSystem.OpenFile(filePath);
This will ensure that you can get the file based on the media file system - which means it will work both for blob storage and a physical filesystem
t
Perfect, many thanks!
3 Views