Skip to main content
CSharpUniversity ASP.NET and C# lessons

The search runs entirely in your browser over the published lesson list.

Uploading a file and checking it

An upload control lets a visitor send a file. Because that file comes from outside, it must be checked before it is trusted.

The control and the save

C#
if (fileUpload.HasFile)
{
    string name = System.IO.Path.GetFileName(fileUpload.FileName);
    string folder = Server.MapPath("~/uploads/");
    fileUpload.SaveAs(System.IO.Path.Combine(folder, name));
    statusLabel.Text = "Stored " + name;
}

What to validate

  • The size, so a huge file cannot fill your disk.
  • The extension and the real content type, not just the name the browser reported.
  • The destination path, so a crafted name cannot escape the folder you intended.
Treat every uploaded file as hostile. Store uploads outside the web root where possible, never execute them, and never trust the file name as given. A custom validator can enforce your rules and show a clear message.

That completes the data control track. Continue to web services and reuse to share the same logic with other applications.

New lessons by email

Leave your address and get a short note whenever a fresh lesson is published. No account needed and you can stop at any time.

Thank you. Your address has been noted in this browser session. Connect a mailing service to deliver the notes for real.