public async Task <FileMetadataDto> UploadFiles(Guid courseId, IFormFile file) { var course = _fileClassesCommunication.GetCourse(courseId); if (course.Result == null || course.Result.Length == 0) { return(null); } // full path to file in temp location FileMetadataDto result = null; if (file.Length > 0) { var filePath = Path.GetTempFileName(); // copy files to temp location for checking using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } // check if files valid if (CheckFileValid(filePath)) { // create folder if it doesn't exists if (!Directory.Exists("../files/" + courseId.ToString() + "/")) { Directory.CreateDirectory("../files/" + courseId.ToString() + "/"); } var path = "../files/" + courseId.ToString() + "/"; // download files to server folder using (var stream = new FileStream(path + file.FileName, FileMode.Create)) { await file.CopyToAsync(stream); } // create metadatas result = new FileMetadataDto { EntityId = Guid.NewGuid(), CourseId = courseId, Path = path, FileName = file.FileName }; // delete temp files after processing if (File.Exists(filePath)) { File.Delete(filePath); } } } var body = "The file '" + result.FileName + "' was added for the course '" + JObject.Parse(course.Result)["courseTitle"] + "'!"; var notification = new Notification { Subject = "A new file was added for one of your courses!", Body = body, Receiver = "*****@*****.**" }; await _fileNotification.SendEmail(notification); return(result); }