Upload file using AngularJS and ASP.Net Core Web API

Last Reply 2 months ago By pandeyism

Posted 2 months ago

I am uploading files but not working with core web api.

How to upload file using web api core and angular

var fileds = HttpContext.Request.Form.Files;
string folderName = "images";
string webRootPath = _hostingEnvironment.WebRootPath;
string newPath = Path.Combine(webRootPath, folderName);
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
for (int i = 0; i < Request.Form.Files.Count; i++)
{
var myFile = Request.Form.Files[i];

if (myFile.Length > 0)
{
string fileName = ContentDispositionHeaderValue.Parse(myFile.ContentDisposition).FileName.Trim('"');
string fullPath = Path.Combine(newPath, fileName);
using (var stream = new FileStream(fullPath, FileMode.Create))
{
myFile.CopyTo(stream);
}
}
}