من که نتونستم چیزی که شما نوشته اید را بخونم ولی کد زیر را خودم دارم استفاده می کنم و امیدوارم مفید باشه :
public ActionResult Index()
{
return View();
}
public ActionResult UploadFile()
{
if (User.Identity.IsAuthenticated)
{
int fileSize = 5242880; // Size limited to 5 MB
try
{
foreach (string file in Request.Files)
{
HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
if (hpf.ContentLength >= fileSize || hpf.ContentLength == 0)
{
// error size exceeding
ViewData["message"] = "File size should be lower than 5 MB.";
return View();
}
// Checking for invalid extentions
string[] ValidExt = { ".png", ".jpg", ".gif", ".doc", ".pdf", ".docx" };
string curExt = System.IO.Path.GetExtension(hpf.FileName).ToLower( );
string curNameWithoutExt = System.IO.Path.GetFileNameWithoutExtension(hpf.Fil eName).ToLower();
var list = from n in ValidExt where n.Equals(curExt) select n;
if (list.Count() == 0 || list == null)
{
// Extention is not Valid
ViewData["message"] = "File Extention is not Valid.";
return Redirect(Request.UrlReferrer.ToString());
//return View();
}
string newFileName = "Upload\\en\\" + curNameWithoutExt + DateTime.Now.ToString().Replace('/', ' ').Replace(':', '_') + DateTime.Now.Millisecond.ToString().Replace(':', '_') + curExt;
string savedFileName = AppDomain.CurrentDomain.BaseDirectory + newFileName;
//Copying the file
hpf.SaveAs(savedFileName);
// Saving in UploadedFiles table
ViewData["Message"] = "Uploaded Succesfully.";
}
}
catch (Exception ex)
{
// To Do : Log error
ViewData["Message"] = "Error in Saving file.";
return View();
}
}
else
{
ViewData["Message"] = "Please login first";
}
return Redirect(Request.UrlReferrer.ToString());
//return View();
}
}