Hi moulalings,
Check this example. Now please take its reference and correct your code.
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
try
{
if (file != null)
{
string path = Server.MapPath("~/Files/");
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
var filename = System.IO.Path.GetFileName(file.FileName);
file.SaveAs(path + filename);
iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(path + filename);
int numberOfPages = pdfReader.NumberOfPages;
TempData["Message"] = "numberOfPages are " + numberOfPages;
if (System.IO.File.Exists(path + filename))
{
System.IO.File.Delete(path + filename);
}
}
}
catch { }
return View();
}
}
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title>
</head>
<body>
<div>
<%using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
<input type="file" name="file" />
<input type="submit" id="btnUpload" value="Upload" />
<br />
<span style="color: green"><%= TempData["Message"]%></span>
<%} %>
</div>
</body>
</html>
Screenshot
