I have developed an ASP.NET core web api application to upload files from one path to other path. I test the api through postman. I would like to display percentage of file uploaded while uploading a file. How to do it on Web API. Any help is appreciated.
[HttpPost]
public IActionResult PostUploadFiles([FromForm] List<IFormFile> postedFiles)
{
try
{
string wwwPath = this.Environment.WebRootPath;
string contentPath = this.Environment.ContentRootPath;
string path = Path.Combine(this.Environment.ContentRootPath, "Uploads");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
List<string> uploadedFiles = new List<string>();
foreach (IFormFile postedFile in postedFiles)
{
string fileName = Path.GetFileName(postedFile.FileName);
using (FileStream stream = new FileStream(Path.Combine(path, fileName), FileMode.Create))
{
postedFile.CopyTo(stream);
FileInfo file = new FileInfo(Path.Combine(path, fileName));
long size = file.Length / 1024;
uploadedFiles.Add(fileName);
}
}
return new ObjectResult("File has been successfully uploaded") { StatusCode = Convert.ToInt32(HttpStatusCode.Created)};
}
catch(Exception ex)
{
return new ObjectResult("File has not been uploaded") { StatusCode = Convert.ToInt32(HttpStatusCode.BadRequest) };
}
}