Skip to content

Instantly share code, notes, and snippets.

@johndavedecano
Created January 31, 2014 01:26
Show Gist options
  • Save johndavedecano/8724965 to your computer and use it in GitHub Desktop.
Save johndavedecano/8724965 to your computer and use it in GitHub Desktop.
Simple File Upload with File Validation with ASP.net C#
@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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment