Advertisement
Intermediate ASP.NET Core, File Uploads & Streaming Large Files

What is the difference between temporary file storage (during processing) and permanent storage for uploaded files, and why should uploads generally not be saved directly to their final destination?

Short answer

Uploads should typically be written to a temporary location first, then validated (file type, virus scan, size checks) and only moved to permanent storage (or a public-facing location) once validation passes -- writing directly to the final destination risks exposing a…

Save ASP.NET Core mock interview
Advertisement
AModel answer
Uploads should typically be written to a temporary location first, then validated (file type, virus scan, size checks) and only moved to permanent storage (or a public-facing location) once validation passes -- writing directly to the final destination risks exposing a partially-uploaded, unvalidated, or malicious file to other parts of the system or other users during the brief window before validation completes.
public async Task<IActionResult> Upload(IFormFile file) {
    var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
    using (var stream = File.Create(tempPath)) {
        await file.CopyToAsync(stream);
    }
    if (!await ValidateFileAsync(tempPath)) {
        File.Delete(tempPath);
        return BadRequest("Invalid file");
    }
    await _storageService.MoveToPermStorageAsync(tempPath);
    return Ok();
}
Real-world example
An upload pipeline writes every file to a temporary location first, running size and content-type validation before ever moving it to the permanent, publicly-accessible storage location, preventing an invalid or malicious file from being briefly exposed.
Likely follow-ups
  • How do you ensure temporary files are cleaned up if validation or the upload process crashes midway?
  • What's the security risk of writing directly to a web-accessible directory during upload?
Related topics