Watch Pre-recorded Live Shows Here
Why Join
Become a member
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
C# Corner Home
Technologies
Monthly Leaders
ASK A QUESTION
Forum guidelines
Raju Fodse
1.9k
235
22.1k
Upload Default File Path if field is empty
Dec 6 2019 1:34 AM
I am using MVC. I have 'Contact' Table in which I have PhotoID filed. I want save data and if Phoid filed is empty then save default path like '~/Uploads/UploadImages/Guest.PNG' how can I do? my code is below
[HttpPost]
[ValidateAntiForgeryToken]
public
ActionResult CreateContacts(Contact contacts, HttpPostedFileBase ImageFile)
/*,TAG2,FLEX1,FLEX2,FLEX3*/
{
if
(ModelState.IsValid)
{
string
fileName = Path.GetFileNameWithoutExtension(ImageFile.FileName);
string
extension = Path.GetExtension(ImageFile.FileName);
fileName = fileName + DateTime.Now.ToString(
"yymmssfff"
) + extension;
contacts.PhotoID =
"~/Uploads/UploadImages/"
+ fileName;
fileName = Path.Combine(Server.MapPath(
"~/Uploads/UploadImages/"
), fileName);
ImageFile.SaveAs(fileName);
using
(ESContext db =
new
ESContext())
{
db.Contacts.Add(contacts);
db.SaveChanges();
return
RedirectToAction(
"IndexContacts"
);
}
}
return
View(contacts);
}
Reply
Answers (
6
)
User login and registration & email notice asp.net mvc
Update Image in MVC