CoreUploadFILE UPLOADS FOR ASP.NET CORE

ASP.NET Core Razor Pages File Upload: Form Binding and Validation

ASP.NET Core · Published · CoreUpload team

Razor Pages makes a small upload form straightforward. The important boundaries are the multipart request, the PageModel property name and the point where an uploaded file becomes accepted application data.

Start with a multipart Razor form

A normal Razor Pages upload posts the file with the rest of a form. Use method="post" and enctype="multipart/form-data". The input name must match the PageModel property. In a project with the standard MVC Tag Helpers imported, asp-for produces that name and the form helper supplies the antiforgery field.

@page
@model UploadModel
<form method="post" enctype="multipart/form-data">
    <label asp-for="Upload">Choose an attachment</label>
    <input asp-for="Upload" type="file" />
    <span asp-validation-for="Upload"></span>
    <button type="submit">Check file</button>
</form>

The label gives the picker an accessible name. The accept attribute can improve the chooser experience, but the server still decides which files are accepted. A form that omitted multipart encoding is not fixed by changing the storage directory.

Bind and validate the current request

[BindProperty]
public IFormFile? Upload { get; set; }

public IActionResult OnPost()
{
    if (Upload == null || Upload.Length == 0)
        ModelState.AddModelError(nameof(Upload), "Choose a non-empty file.");
    if (Upload != null && Upload.Length > 10 * 1024 * 1024)
        ModelState.AddModelError(nameof(Upload), "The file must be 10 MB or smaller.");

    if (!ModelState.IsValid) return Page();
    // This binding example intentionally does not save the file.
    return Page();
}

This PageModel fragment demonstrates binding and feedback only. Add storage, content validation and authorization for your application before using an upload endpoint in production. The length check is an application rule, not a guarantee that the host or proxy will accept a request of that size. Those request limits are configured separately.

Decide how the file becomes permanent

Generate a storage key on the server, keep uploads away from executable application content and store the original file name only as display metadata. Inspect content as well as the extension. If validation or scanning is asynchronous, keep the object in a pending state and make the UI reflect that status.

A file can arrive successfully while the accompanying business form is still invalid. For multi-step forms, consider uploading to temporary storage first and committing the attachment only when the record is saved. Expire abandoned objects without deleting active or committed files.

Use CoreUpload when the browser needs a queue

For progress, multiple files or chunked transfers, the CoreUpload Tag Helper keeps uploader configuration in Razor markup. In an application already configured with AddCoreUpload, UseCoreUpload and MapCoreUploadEndpoints, a basic component looks like this:

<core-upload asp-upload-url="/api/upload/upload"
             asp-multiple="true"
             asp-progress="true"
             asp-drop-zone="true"></core-upload>

Include the package's Tag Helper import and static assets as shown in Getting Started. The component endpoint is a separate HTTP request: do not expect its files to appear automatically in a later Razor Page IFormFile property. Carry server-validated upload identifiers into the final record-save operation.

When the file property is unexpectedly null

  • Check the actual multipart payload and input name in the browser network panel.
  • Confirm the PageModel property is bound and the handler is the intended OnPost method.
  • Distinguish a rejected request size from an empty picker.
  • Check the response status before attempting to parse JSON or show success.
  • Test a missing file, a zero-byte file, an allowed file and a file above the policy limit.

Common questions

Can I post the file as application/json?

A browser File object is not JSON file content. Use multipart FormData for a file plus form values, or design an explicit binary endpoint.

Should every upload use streaming?

No. Buffered binding is often adequate for smaller files. Choose streaming or chunked transport based on measured memory use, request limits and recovery needs.

Official references

Examples describe the stated framework and application workflow. Check your installed component version and hosting configuration before adapting them.

Evaluate CoreUpload for ASP.NET Core