Base solution for your next web application
Open Closed

Files through WebApi #1508


User avatar
0
ddnils created

Hi there, I would like to post a feature request. What do you think about a File-Storage in database? I would like to propose file storage (pdf, jpeg, jpg, etc.) in database with a new Entity class (FileEntity). It should save files in database as bytearrays. For additional properties I propose fileSize and fileType or fileExtension. The FileEntity should be served by a webapi and have overloads for images (possibly for different resolutions).

I will try to implement a demo of this in the coming weeks and come back, but what do you think?


2 Answer(s)
  • User Avatar
    0
    ddnils created

    This is what I would suggest as FileEntity:

    namespace Abp.Application.Services.Dto
    {
        /// <summary>
        /// saves files in database
        /// </summary>
        /// <typeparam name="TPrimaryKey"></typeparam>
        [Serializable]
        public class FileEntityDtoOfTPrimaryKey<TPrimaryKey>: IEntityDto<TPrimaryKey>
        {
            /// <summary>
            /// Id of the entity.
            /// </summary>
            public TPrimaryKey Id { get; set; }
            /// <summary>
            /// This holds the bytearray contents of the file
            /// </summary>
            public Byte[] FileContent { get; set; }
            /// <summary>
            /// holds the size of the file in bytes
            /// </summary>
            public int FileSize { get; set; }
            /// <summary>
            /// holds the filename (without extension) 
            /// </summary>
            public string FileName { get; set; }
            /// <summary>
            /// holds the extension or filetype
            /// </summary>
            public string FileExtension { get; set; }
    
            /// <summary>
            /// 
            /// </summary>
            protected FileEntityDtoOfTPrimaryKey()
            {
                if (this.FileContent != null)
                {
                    this.FileSize = FileContent.Length;
                }
            }
            /// <summary>
            /// 
            /// </summary>
            protected FileEntityDtoOfTPrimaryKey(TPrimaryKey id)
            {
                this.Id = id;
                if (this.FileContent != null)
                {
                    this.FileSize = FileContent.Length;
                }
            }
        }
    }
    
  • User Avatar
    0
    ddnils created

    In the end I would like to have a "stretch goal" where I can get images through a web-api call like this:

    <abpApp>/api/images/<imageIdOrName>?width=200&height=300

    this could prove to be very useful if I have to load images in different resolutions for different media types (mobile to desktop). Of course caching should be implemented here... maybe even save different (default) sizes of an image inside db for specially indicated images like background images, logos, etc. (we could specify these images with a selector-attribute).