예제 #1
0
        /// <summary>
        /// Saves an ExcelPackage as a BinaryFile and stores it in the database
        /// </summary>
        /// <param name="excel">The ExcelPackage object to save</param>
        /// <param name="fileName">The filename to save the workbook as</param>
        /// <param name="rockContext">The RockContext to use</param>
        /// <param name="binaryFileType">Optionally specifies the BinaryFileType to apply to this file for security purposes</param>
        /// <returns></returns>
        public static BinaryFile Save( ExcelPackage excel, string fileName, RockContext rockContext, BinaryFileType binaryFileType = null )
        {
            if ( binaryFileType == null )
            {
                binaryFileType = new BinaryFileTypeService( rockContext ).Get( Rock.SystemGuid.BinaryFiletype.DEFAULT.AsGuid() );
            }

            var ms = excel.ToMemoryStream();

            var binaryFile = new BinaryFile()
            {
                Guid = Guid.NewGuid(),
                IsTemporary = true,
                BinaryFileTypeId = binaryFileType.Id,
                MimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                FileName = fileName,
                ContentStream = ms
            };

            var binaryFileService = new BinaryFileService( rockContext );
            binaryFileService.Add( binaryFile );
            rockContext.SaveChanges();
            return binaryFile;
        }