Reading .csv file which is uploaded to EPiServer CMS

Vote:
 

Hello,

We have requirement to bulk update the meta tag details from a .csv file. We have a contentreference field to hold the .csv file and fetching the file using contentrepository. Now trying to read the file using streamreader() or file.readalltext() which will not work in this context.

Can any one guide how to deal this scenario?

Many Thanks,

Deepa

#289009
Oct 11, 2022 14:43
Vote:
 

Try this! 😁

[UIHint(UIHint.MediaFile)]
public virtual ContentReference CsvFile { get; set; }

public string GetCsvFileContent()
{
    if (!ContentReference.IsNullOrEmpty(CsvFile))
    {
        var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
        var contentFile = contentLoader.Get<MediaData>(CsvFile);
        using (var sr = new StreamReader(contentFile.BinaryData.OpenRead()))
        {
            return sr.ReadToEnd();
        }
    }
    return null;
}
#289013
Oct 11, 2022 15:51
Deepa Puranik - Oct 12, 2022 5:12
Thank you :-)
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.