Introduction
In modern web applications, file uploads are a common feature — whether it’s invoices, employee documents, or product images.
But every file upload carries a security risk. A single malicious file can compromise your entire system.
That’s why integrating virus scanning during the file upload process is not optional anymore — it’s essential.
In this article, we’ll explore how to build a secure file upload system using Angular (frontend) and ASP.NET Core (backend) with ClamAV or an external antivirus scanning API to detect threats before storing files.
Why File Scanning Matters
Malicious users often upload:
Executable files disguised as PDFs or images
Files containing embedded malware or scripts
Corrupted data that crashes backend processing
If these files are stored unchecked, they can cause:
System compromise
Data corruption
Application downtime
By integrating ClamAV or a cloud-based virus scanning API, you ensure each file is verified before it reaches your database or file server.
System Architecture
Here’s how the process works step by step:
Angular File Upload Component
│
▼
ASP.NET Core File Upload API
│
▼
Virus Scanner (ClamAV / External API)
│
├── Clean → Store File (Database / Cloud / File System)
└── Infected → Reject File & Log Event
Core Components
Angular Frontend: Captures and uploads files.
ASP.NET Core API: Receives and streams the file securely.
ClamAV (or API): Scans uploaded files in real-time.
Storage Layer: Saves only clean files to disk or cloud.
Step 1: File Upload in Angular
A simple Angular component for file upload:
@Component({
selector: 'app-file-upload',
template: `
<h3>Upload File</h3>
<input type="file" (change)="onFileSelected($event)" />
`
})
export class FileUploadComponent {
constructor(private http: HttpClient) {}
onFileSelected(event: any) {
const file = event.target.files[0];
if (file) {
const formData = new FormData();
formData.append('file', file);
this.http.post('https://yourapi.com/api/files/upload', formData)
.subscribe({
next: res => alert('File uploaded successfully!'),
error: err => alert('Upload failed: ' + err.message)
});
}
}
}
Step 2: File Upload API in ASP.NET Core
Create a secure upload endpoint that temporarily stores and scans the file.
[ApiController]
[Route("api/files")]
public class FileUploadController : ControllerBase
{
private readonly IVirusScannerService _virusScanner;
public FileUploadController(IVirusScannerService virusScanner)
{
_virusScanner = virusScanner;
}
[HttpPost("upload")]
public async Task<IActionResult> UploadFile(IFormFile file)
{
if (file == null || file.Length == 0)
return BadRequest("Invalid file");
var tempPath = Path.Combine(Path.GetTempPath(), file.FileName);
using (var stream = new FileStream(tempPath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
var isClean = await _virusScanner.ScanFileAsync(tempPath);
if (!isClean)
{
System.IO.File.Delete(tempPath);
return BadRequest("Virus detected! Upload rejected.");
}
// Move to permanent storage
var finalPath = Path.Combine("C:\\Uploads", file.FileName);
System.IO.File.Move(tempPath, finalPath);
return Ok("File uploaded and scanned successfully.");
}
}

Join the conversation! Your thoughts help the community grow.