Try our conversational search powered by Generative AI!

Programmatically creating a media file that can be downloaded

Vote:
 

I'm creating a media file (text or, preferrably xml) in a scheduled job but the created file cannot be downloaded from the media folder. The code I use to create the file:

string fileExtension = ".txt";
Type mediaType = this.contentMediaResolver.GetFirstMatching(fileExtension);
ContentType contentType = contentTypeRepository.Load(mediaType);
GenericMedia golfCourseFile = this.contentRepository.GetDefault(SiteDefinition.Current.GlobalAssetsRoot, contentType.ID);
golfCourseFile.Name = string.Format("{0}{1}{2}", this.fileNamePrefix, "GolfCourses", fileExtension);
Blob golfCourseBlob = this.blobFactory.CreateBlob(golfCourseFile.BinaryDataContainer, fileExtension);
XDocument golfCourseDocument = new XDocument(new XElement("Courses"));

// populate XDocument

using (Stream stream = golfCourseBlob.OpenWrite())
{
    golfCourseDocument.Save(stream);
}

this.contentRepository.Save(golfCourseFile, SaveAction.Publish, AccessLevel.NoAccess);

Then in the CMS I run the job but the download option on the generated file is disabled. How can I generate the file from the scheduled job and allow editors to download the file from the media library?

Thanks,

#191048
Edited, Apr 19, 2018 13:08
Vote:
 

I think you need to specify the content of the file immediately above the save: 

golfCourseFile.BinaryData = golfCourseBlob;
this.contentRepository.Save(golfCourseFile, SaveAction.Publish, AccessLevel.NoAccess);
#191056
Edited, Apr 19, 2018 14:25
Vote:
 

Thank you Darren!

#191060
Apr 19, 2018 15:07
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.