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.