Created
June 27, 2019 09:34
-
-
Save felipebossolani/7b037b1f4ebbc8e8a31318c38ad0390d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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