Introduction
In this article, we will see how to upload a file into the database using the Angular app in an ASP.NET project. Let us create a new project in Visual Studio 2017. We are using ASP.NET Core 2.1 and Angular 5.2 for this project.
Step 1
Open VS2017 and create a new project >>Web >> .NET Core >> ASP.NET Core web application. Now, select the Angular app template and click OK.
Step 2
Now, right click your ClientApp folder and select "Open containing folder".
Step 3
Write cmd on the path and enter and run > npmInstall
Now, your command prompt is open. Write the npm installation command for installing your packages in the Angular app.
In this article, we will see how to upload a file into the database using the Angular app in an ASP.NET project. Let us create a new project in Visual Studio 2017. We are using ASP.NET Core 2.1 and Angular 5.2 for this project.
Step 1
Open VS2017 and create a new project >>Web >> .NET Core >> ASP.NET Core web application. Now, select the Angular app template and click OK.

Step 2
Now, right click your ClientApp folder and select "Open containing folder".
Step 3
Write cmd on the path and enter and run > npmInstall

Now, your command prompt is open. Write the npm installation command for installing your packages in the Angular app.
npm install
Step 4
Now, run your application and it automatically restores your npm packages.
See your project structure given below.
Step 5
Now, add the file and upload API Controller in your project.
Step 4
Now, run your application and it automatically restores your npm packages.
See your project structure given below.

Step 5
Now, add the file and upload API Controller in your project.
Right click on controller>>new item>>WebApiControllre>>give name UploadController.cs
Step 6
Copy the following code into API controller.

Step 6
Copy the following code into API controller.
- Open fileupload controller and paste this code.
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net.Http.Headers;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Mvc;
- // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
- namespace FileUploadAngular5WithAsp.NetCore.Controllers
- {
- [Produces("application/json")]
- [Route("api/[controller]")]
- public class UploadController : Controller
- {
- private IHostingEnvironment _hostingEnvironment;
- public UploadController(IHostingEnvironment hostingEnvironment)
- {
- _hostingEnvironment = hostingEnvironment;
- }
- [HttpPost, DisableRequestSizeLimit]
- public ActionResult UploadFile()
- {
- try
- {
- var file = Request.Form.Files[0];
- string folderName = "Upload";
- string webRootPath = _hostingEnvironment.WebRootPath;
- string newPath = Path.Combine(webRootPath, folderName);
- if (!Directory.Exists(newPath))
- {
- Directory.CreateDirectory(newPath);
- }
- if (file.Length > 0)
- {
- string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
- string fullPath = Path.Combine(newPath, fileName);
- using (var stream = new FileStream(fullPath, FileMode.Create))
- {
- file.CopyTo(stream);
- }
- }
- return Json("Upload Successful.");
- }
- catch (System.Exception ex)
- {
- return Json("Upload Failed: " + ex.Message);
- }
- }
- }
- }



JosephsPosted Jul 15, 2022, 5:08 AM
Github not working
Saikrishna SurapaneniPosted Aug 20, 2019, 12:18 AM
Good one, it helped me a lot. I used IFormFile and all, but nothing worked.
Norberth BrunnerPosted Nov 15, 2018, 10:59 AM
Hi! Can my applicattion on Angular Cancel the upload process? Thanks
Per GradeenPosted Oct 4, 2018, 2:49 AM
I cannot upload a file of size 60 MB, 15 MB or lower works.
Thakka KPosted Oct 4, 2018, 2:04 AM
I've been confused not knowing how to recieve Request.Form.Files in controllers until I read this artilcle. It's very helpful for me, thank you.
Viknaraj ManogararajahPosted Jul 14, 2018, 11:01 PM
Nice Article, Thank you for sharing.........
Christian MorantePosted Jul 9, 2018, 9:54 AM
Congrats for your effort for make this article
Sanghamitra MohantyPosted Jul 7, 2018, 9:16 PM
Nice one....
Bhairab DuttPosted Jul 7, 2018, 1:40 PM
Nice Article...