ASP Core WebApi Test File Upload using Postman

后端 未结 4 1218
清酒与你
清酒与你 2020-12-06 00:20

I have created an endpoint that takes an arbitrary file:

[HttpPost()]
public async Task CreateFile(IFormFile file)

Whe

相关标签:
4条回答
  • 2020-12-06 01:05

    Thanks to @rmjoia's comment I got it working! Here is what I had to do in Postman:

    0 讨论(0)
  • 2020-12-06 01:05
       [HttpPost("UploadSingleFile"), Route("[action]")]
        public async Task<IActionResult> UploadSingleFile([FromForm(Name = "file")] IFormFile file)
        {
    
    
            // Process uploaded files
    
            string folderName = "Uploads";
            string webRootPath = hostingEnvironment.WebRootPath;
            string newPath = Path.Combine(webRootPath, folderName);
            if (!Directory.Exists(newPath))
            {
                Directory.CreateDirectory(newPath);
            }
    
            Repository.Models.File fileModel = new Repository.Models.File();
            fileModel.Name = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
            fileModel.Path = $"{folderName}/{file.FileName}";
            fileModel.Size = file.Length;
            fileModel.Type = file.ContentType;
    
            string fullPath = Path.Combine(newPath, fileModel.Name);
    
            fileModel.Extension = Path.GetExtension(fullPath);
            fileModel.CreatedDate = Utility.Common.GetDate;
            fileModel.CreatedBy = 1;
    
            //fileModel save to db
    
            using (var stream = new FileStream(fullPath, FileMode.Create))
            {
                //file.CopyTo(stream);
                await file.CopyToAsync(stream);
            }
    
            return Ok(new { count = 1, path = filePath });
        }
    
    0 讨论(0)
  • 2020-12-06 01:07

    Your should be like that

     [HttpPost]       
       public async Task<IActionResult> UploadFile([FromForm]UploadFile updateTenantRequest)
            {
    }
    

    Your class should be like:-

    public class UpdateTenantRequestdto
        {
    
    
            public IFormFile TenantLogo { get; set; }
        }
    

    and then

    0 讨论(0)
  • 2020-12-06 01:12

    The complete solution for uploading file or files is shown below:

    • This action use for uploading multiple files:

      // Of course this action exist in microsoft docs and you can read it.
      HttpPost("UploadMultipleFiles")]
      public async Task<IActionResult> Post(List<IFormFile> files)
      {
      
          long size = files.Sum(f => f.Length);
      
          // Full path to file in temp location
          var filePath = Path.GetTempFileName();
      
          foreach (var formFile in files)
          {
              if (formFile.Length > 0)
                  using (var stream = new FileStream(filePath, FileMode.Create))
                      await formFile.CopyToAsync(stream);
          }
      
          // Process uploaded files
      
          return Ok(new { count = files.Count, path = filePath});
      }
      

    The postman picture shows how you can send files to this endpoint for uploading multiple files:

    • This action use for uploading single file:

      [HttpPost("UploadSingleFile")]
      public async Task<IActionResult> Post(IFormFile file)
      {
      
          // Full path to file in temp location
          var filePath = Path.GetTempFileName();
      
          if (file.Length > 0)
              using (var stream = new FileStream(filePath, FileMode.Create))
                  await file.CopyToAsync(stream);
      
          // Process uploaded files
      
          return Ok(new { count = 1, path = filePath});
      }
      

    The postman picture shows how you can send a file to this endpoint for uploading single file:

    0 讨论(0)
提交回复
热议问题