Created
January 31, 2014 01:26
-
-
Save johndavedecano/8724965 to your computer and use it in GitHub Desktop.
Simple File Upload with File Validation with ASP.net C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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