Created
June 27, 2019 10:14
-
-
Save felipebossolani/744f99d10077e12a5d2f302d1dea0e96 to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
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/files", 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(Exception e) | |
{ | |
return BadRequest(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment