liveBook · Manning

File upload

As explained in ASP.NET Core in Action, 3e

A file upload is the transfer of a file from an HTTP client to an application through a request.

A file upload transfers a file from an HTTP client to an application through a request. ASP.NET Core binds uploaded files to IFormFile for one file or IFormFileCollection for multiple files. section 7.6.3

Treat uploads as untrusted input and decide whether the feature is necessary before implementing it:

  • Do not trust the submitted filename; generate a new name before saving the file. section 7.6.3
  • Plan carefully for malicious files, oversized requests, accidental denial of service, and files being executed on the server. section 7.6.3 section 16.2.2
  • Choose storage deliberately among a database, the filesystem, or another storage system. section 7.6.3
  • Use IFormFile for small files because ASP.NET Core buffers the complete file in memory and on disk before the handler receives it. section 7.6.3

Binding one or several files

A minimal API handler can accept an IFormFile parameter for one upload or an IFormFileCollection parameter for several uploads. Razor Pages can bind IFormFile, IFormFileCollection, IEnumerable<IFormFile>, or List<IFormFile> as a handler parameter or binding property. section 7.6.3 section 16.2.2

public interface IFormFile
{
   string ContentType { get; }
   long Length { get; }
   string FileName { get; }
   Stream OpenReadStream();
}

Inspect the IFormFile members used to read upload metadata and open a stream, including ContentType, Length, FileName, and OpenReadStream. section 7.6.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains binding interfaces, metadata access, buffering, storage choices, and security risks without detailing complete persistence implementation.
Examples
Uses IFormFile, IFormFileCollection, IEnumerable<IFormFile>, List<IFormFile>, and the IFormFile metadata interface.
What it assumes
Assumes familiarity with ASP.NET Core request handlers or Razor Pages and introduces file binding through framework-provided interfaces.
What you can do after
Apply file binding through IFormFile or collection types and assess basic security and storage decisions.
How it connects
Connects file uploads with HTTP requests, model binding, minimal APIs, Razor Pages, validation, storage, and application security.

Also explained in

Compare how 5 books explain file upload →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.