Search code examples
c#asp.net-coreasp.net-web-apipostman

Uploading files with two different keys in Asp.net Core Web API and Postman


I have a Table (Course),two columns of CourseTable(ImageName and FileName) should have Course Image and course File, I can post file successfully, but image returns null always. My question is, how can I post two files, with two different keys ? Also if I want to send more than one file for CourseFile, how can I do that?

 public class FIleUploadAPI
    {
        public IFormFile FileName { get; set; }
        public IFormFile ImageName { get; set; }
        public string courseDto { get; set; }

    }

  [HttpPost]
    public async Task<ApiResult<CourseResultDtoForPublic>> Create([FromForm] FIleUploadAPI objfile, CancellationToken cancellationToken)
    {

        await UploadFile(objfile);
        CourseDto courseDtoDeserialize = JsonConvert.DeserializeObject<CourseDto>(objfile.courseDto);
        courseDtoDeserialize.FileName = objfile.FileName.ToString();
        courseDtoDeserialize.ImageName = objfile.ImageName.ToString();
        var model = courseDtoDeserialize.ToEntity(mapper);

        await courseRepository.AddAsync(model, courseDtoDeserialize.MajorId, cancellationToken);

        var dto = await courseRepository.TableAsNoTracking.ProjectTo<CourseResultDtoForPublic>(mapper.ConfigurationProvider)
             .FirstOrDefaultAsync(c => c.Id == model.Id, cancellationToken);
        return dto;



    }

    [HttpPost]
    [Route("/api/Course/UploadFile")]
    //[RequestFormLimits(MultipartBodyLengthLimit = 209715200)]
    //[RequestSizeLimit(209715200)]
    public async Task<string> UploadFile([FromForm]FIleUploadAPI objfile)
    {
        if (objfile.FileName.Length > 0)
            
        {
            try
            {
                if (!Directory.Exists(_environment.WebRootPath + "\\uploads\\"))
                {
                    Directory.CreateDirectory(_environment.WebRootPath + "\\uploads\\");
                }
                using (FileStream filestream = System.IO.File.Create(_environment.WebRootPath + "\\uploads\\" + objfile.FileName.FileName))
                {
                    objfile.FileName.CopyTo(filestream);
                    filestream.Flush();
                    return "\\uploads\\" + objfile.FileName.FileName;
                }
            }
            catch (Exception ex)
            {
                 return ex.ToString();
            }
        }

        if (objfile.ImageName.Length > 0)

        {
            try
            {
                if (!Directory.Exists(_environment.WebRootPath + "\\uploads/CourseImages\\"))
                {
                    Directory.CreateDirectory(_environment.WebRootPath + "\\uploads/CourseImages\\");
                }
                using (FileStream filestream = System.IO.File.Create(_environment.WebRootPath + "\\uploads/CourseImages\\" + objfile.ImageName.FileName))
                {
                    objfile.ImageName.CopyTo(filestream);
                    filestream.Flush();
                    return "\\uploads/CourseImages\\" + objfile.ImageName.FileName;
                }
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
        
        else
        {
            return "Unsuccessful";
        }
        


    }

enter image description here


Solution

  • public IFormFile ImageName { get; set; }

    CourseDto and FileName Parameters are bound correct from postman,but ImageName return null value to my action parameter

    Please note that model binding looks for matches to property_name (or parameter_name) from the value(s) in the request, in your screenshot we can find you specify the key with ImagName as below, not ImageName, which cause it can not bind the value to property well.

    enter image description here

    Please try to change it to ImageName in postman, then it should work as expected.

    For more information about how model binding works, please check this doc:

    https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-5.0