0% found this document useful (0 votes)
45 views

CSHTML File - File Upload

This document provides code and HTML for a simple file upload form in ASP.NET C# that validates the file type. It accepts only JPEG, PNG, and GIF files, saves valid files to the App_Data folder, and displays an error for invalid file types. The code checks the file extension against a list of allowed extensions and saves the file if it matches or displays an error message if not.

Uploaded by

hsuyip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

CSHTML File - File Upload

This document provides code and HTML for a simple file upload form in ASP.NET C# that validates the file type. It accepts only JPEG, PNG, and GIF files, saves valid files to the App_Data folder, and displays an error for invalid file types. The code checks the file extension against a list of allowed extensions and saves the file if it matches or displays an error message if not.

Uploaded by

hsuyip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Simple File Upload with File Validation with ASP.net C# .

This includes code as


well as html.

@using System.Collections
@{

if(IsPost)
{
if(Request.Files.Count > 0)
{
string[] allowed = {".jpg",".png",".gif"};
var uploaded = Request.Files[0];
var extension = System.IO.Path.GetExtension(uploaded.FileName);
if(allowed.Contains(extension))
{
var fileSavePath = Server.MapPath("~/App_Data/uploads/" +
uploaded.FileName);
uploaded.SaveAs(fileSavePath);
}else{
<div>File is not allowed</div>
}
}
}
}

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="userfile">
<input type="submit" value="Upload File">
</form>
</body>
</html>

You might also like