Skip to content

Instantly share code, notes, and snippets.

@felipebossolani
Created June 27, 2019 09:34
Show Gist options
  • Save felipebossolani/7b037b1f4ebbc8e8a31318c38ad0390d to your computer and use it in GitHub Desktop.
Save felipebossolani/7b037b1f4ebbc8e8a31318c38ad0390d to your computer and use it in GitHub Desktop.
using AspNetCoreMultipleUploadAPI.WebApi.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace AspNetCoreMultipleUploadAPI.WebApi.Controllers
{
[Route("api/file")]
public class FileController : Controller
{
[HttpPost("upload")]
public async Task<IActionResult> Upload(List<IFormFile> files)
{
try
{
var result = new List<FileUploadResult>();
foreach (var file in files)
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images", file.FileName);
var stream = new FileStream(path, FileMode.Create);
file.CopyToAsync(stream);
result.Add(new FileUploadResult() { Name = file.FileName, Length = file.Length });
}
return Ok(result);
}
catch
{
return BadRequest();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment