如何解决引用的对象为Null-HTTPPostedFiles
伙计们,
我试图使用ASP.NET MVC代码优先方法将图像上传到文件夹并将路径URL保存在数据库中。
在保存URL路径时,它返回对象引用为Null。
下面是保存表单时调用的操作方法。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Account_Number,Title,Firstname,Othername,Surname,Phone_Number,Bank_AccountName,Bank_Fullname,img_Passport")] SavingsAccount savingsAccount,HttpPostedFileBase postedFile)
{
Thread.Sleep(2000);
//Extract Image File Name.
string fileName = "1000000005";
//Set the Image File Path.
string filePath = "~/Images/Uploads/SavingsAccount/" + fileName;
//Save the Image File in Folder.
postedFile.SaveAs(Server.MapPath(filePath));
if (ModelState.IsValid)
{
savingsAccount.Account_Number = Account_No;
savingsAccount.Account_Type = "Savings Account";
savingsAccount.Account_Balance = 0;
savingsAccount.Date_Opened = Convert.ToDateTime(DateTime.Now.ToString());
savingsAccount.Opened_By = User.Identity.Name;
savingsAccount.img_Passport = filePath; //Insert the Passport URL Path to database
db.SavingsAccounts.Add(savingsAccount);
db.SaveChanges();
return View("DisplaySuccessMessage");
}
return View(savingsAccount);
}
当我使用断点检查发送的值时,Posted文件被视为Null。
我做错了吗?
解决方法
在ASP.NET Core中使用上载文件,例如this article
.content {
max-width: 700px;
margin: 40px auto 10px;
padding: 0 15px;
font-size: 16px;
line-height: 1.7;
color: #333;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。