mahmoud nezar sarhan
mahmoud nezar sarhan

Reputation: 756

How to upload a file with other data in ASP.NET Core Boilerplate Application service?

I have an ASP.NET Core boilerplate application service that supposed to receive a file and store it in the database with other various set of data. The file is represented as a byte[] and the others are strings, integers and lists.

// Application service
public class JournalsService : ApplicationService, IJournalsService
{
        private IJournalsManager _manager;

        public JournalsService(IJournalsManager manager)
        {
            _manager = manager;
        }

        public JournalDTO Get(int id)
        {
            return AutoMapper.Mapper.Map<Journal, JournalDTO>(_manager.Get(id));
        }

        public JournalDTO Update(JournalUpdateDTO journal)
        {
            Journal temp = AutoMapper.Mapper.Map<JournalUpdateDTO, Journal>(journal);
            temp = _manager.Update(temp);
            return AutoMapper.Mapper.Map<Journal, JournalDTO>(temp);
        }

        public JournalDTO Create(JournalCreateDTO journal)
        {
            Journal temp = AutoMapper.Mapper.Map<JournalCreateDTO, Journal>(journal);
            _manager.Create(temp);
            return AutoMapper.Mapper.Map<Journal, JournalDTO>(temp);
        }

        public void Delete(int id)
        {
            _manager.Delete(id);
        }
}

Both the entity and the DTOs have the file as byte[] and the other fields are string, int and lists.

    // Create DTO
    [AutoMap(typeof(Models.Journal))]
    public class JournalCreateDTO
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public string ISSNPrint { get; set; }
        public string ISSNOnline { get; set; }
        public List<string> FieldsOfInterests { get; set; }
        public byte[] Image { get; set; }
    }

and the entity :

 // Entity
 public class Journal : Entity, IHasCreationTime
 {
        public string Name { get; set; }
        public string Description { get; set; }
        public DateTime CreationTime { get; set; }
        public string ISSNPrint { get; set; }
        public string ISSNOnline { get; set; }
        public string FieldsOfInterests { get; set; }
        public byte[] Image { get; set; }
        public ICollection<Article> Articles { get; set; }

        public Journal()
        {
            CreationTime = Clock.Now;
        }
}

In swagger UI the file field required to be a string (maybe a string of bytes) but i need it to be a byte[] and i need the file upload to be in the application service . I searched in the boilerplate documentation and searched in official forum but with no luck .

So how to implement a method for receive a file upload ??

Upvotes: 4

Views: 1511

Answers (1)

Edward
Edward

Reputation: 29986

For uploading file in swagger UI, you will need to change application/json to form-data.

Follow steps below:

  1. Add FileOperationFilterSource Code to support form-data.
  2. Register FileOperationFilter in Startup.cs

        services.AddSwaggerGen(options =>
        {
            options.OperationFilter<FileOperationFilter>();
    
            options.SwaggerDoc("v1", new Info { Title = "EdwardAbp API", Version = "v1" });
            options.DocInclusionPredicate((docName, description) => true);
    
            // Define the BearerAuth scheme that's in use
            options.AddSecurityDefinition("bearerAuth", new ApiKeyScheme()
            {
                Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                Name = "Authorization",
                In = "header",
                Type = "apiKey"
            });
            options.DescribeAllEnumsAsStrings();
            // Assign scope requirements to operations based on AuthorizeAttribute
            options.OperationFilter<SecurityRequirementsOperationFilter>();
        });
    
  3. Change public byte[] Image { get; set; } to

     public IFormFile Image { get; set; }
    
  4. Change Operation by adding [FromForm] for the model which contians IFormFile property.

    public async Task<OrderDto> CreateOrder([FromForm]OrderDto input)
    {
    
    }
    
  5. After accessing swagger, you will be able to upload file by click Choose File.
    enter image description here

Upvotes: 3

Related Questions