Controler
[HttpPost]
public ActionResult Index(Add_Book _books)
{
BusinessLayer booksBusinessLayer = new BusinessLayer();
//books file = new books();
//var book = new books();
receiving error at below line
var fileName = Path.GetFileName(_books.File.FileName);
//var fileName = book.File.FileName;
var path = Path.Combine(Server.MapPath("~/Osho_Images"), fileName);
book.File.SaveAs(path);
booksBusinessLayer.AddBooks(_books);
return View();
}
Modal
public class books
{
public string ID
{
get;
set;
}
public string ISBN
{
get;
set;
}
public string Book_Title
{
get;
set;
}
public string Book_Cat
{
get;
set;
}
public string Language
{
get;
set;
}
public string Book_Desc
{
get;
set;
}
public string Price
{
get;
set;
}
public string Book_Img
{
get;
set;
}
public HttpPostedFileBase File
{
get;
set;
}
public int Qty
{
get;
set;
}
public int Qty_Alert
{
get;
set;
}
}
Pleas provide the solution.
Like David Tansey say, _books.File.FileName assume that is NOT null, so you need to validate before to try get access to the object,
When I worked with upload files I did something like the following code, hope this help you
just use in your view
<input type="file" />
then in your controller, in this case i put the for, because the user can upload multiple files,
[HttpPost]
public ActionResult SaveFile(YourModel model)
{
foreach (string file in Request.Files)
{
SaveYourFile(Request.Files[file]);
}
return View();
}
private void SaveYourFile(HttpPostedFileBase file)
{
if(file.ContentLenght >0)
{
//now you can access to your uploaded file
var book = new books();
var path = Path.Combine(Server.MapPath("~/Osho_Images"), file.fileName);
book.File.SaveAs(path);
booksBusinessLayer.AddBooks(_books);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.