Skip to content

Instantly share code, notes, and snippets.

@aliab
Last active December 16, 2015 05:28
Show Gist options
  • Save aliab/5384247 to your computer and use it in GitHub Desktop.
Save aliab/5384247 to your computer and use it in GitHub Desktop.
multifile uploader in razor engine and ASP.NET
@{
var newFileName = "";
var imagePath = "";
var fileName="";
var path = HttpContext.Current.Server.MapPath("~/");
if(IsPost){
// make time instance to make folder base on time
System.DateTime moment = DateTime.Now;
string timeFolderName = moment.Day + "-" + moment.Month + "-" + moment.Year;
// make HttpPostedFileBase variable to get files into it
HttpPostedFileBase uploadedFile = null;
// make a list of image variable to join and insert in databse
var myimages = new List<string>();
for(var i=0; i < Request.Files.Count ; i++){
uploadedFile = Request.Files[i];
fileName = timeFolderName + "_" + Path.GetFileName(uploadedFile.FileName) + "__" + Guid.NewGuid().ToString();
//make folder base on date
if( !Directory.Exists(path+@"\images\"+ timeFolderName ) ){
Directory.CreateDirectory(path + @"\images\"+ timeFolderName );
}
if (fileName != string.Empty)
{
WebImage photos = new WebImage(uploadedFile.InputStream);
// this is the ppath of saved image
imagePath = @"images/"+ timeFolderName + "/" + fileName;
photos.Save(@"~/"+ imagePath);
myimages.Add(fileName);
}
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>My Site's Title</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
</head>
<body>
@path
<form method="post" enctype="multipart/form-data">
<p>
@FileUpload.GetHtml(
initialNumberOfFiles:1,
allowMoreFilesToBeAdded:true,
includeFormTag:false)
</p>
<input type="submit" value="submit" />
</form>
</body>
</html>
@aliab
Copy link
Author

aliab commented Apr 29, 2014

remember to install Aspnet web helper library form Nuget before use this code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment