TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
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
Forums
Monthly Leaders
Forum guidelines
Raju Fodse
1.5k
244
31.3k
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