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
Mark Tabor
587
2k
460.5k
server.MapPath in c# mvc
May 18 2021 4:24 AM
Hi I have an application which works fine when i run it using visual studio, I am saving employee name , photo and email it works fine and save the file to the directory in my project -->employee Photo Folder , Now when I deploy this application to IIS ( i have checked to put the published folder on DESKTOP as well as in wwwroot
folder as well .
but when saving the record from the deployed application it did not get saved and i am unable to debug as it is deployed as per my understanding server.MAPPATH does not work on iss
below is the code which works fine with visual studio
if
(upload !=
null
&& upload.ContentLength > 0 && upload.ContentLength < 41943040)
{
var avatar =
new
File
{
FileName = System.IO.Path.GetFileName(upload.FileName),
FileType = FileType.Avatar,
ContentType = upload.ContentType
};
File fl =
new
File();
fl.FileName = System.IO.Path.GetFileName(upload.FileName);
string
pic = System.IO.Path.GetFileName(upload.FileName);
string
path = System.IO.Path.Combine(
Server.MapPath(
"~/StudentPhotos/"
), pic);
var allowedExtensions =
new
[] {
".jpg"
,
".png"
,
".jpeg"
,
".gif"
,
".svg"
};
// check file is empty or not
if
(upload !=
null
)
{
// check the bytes of file is greater than 0
if
(upload.ContentLength > 0)
{
// get the uploaded file extension
string
ext = System.IO.Path.GetExtension(upload.FileName).ToLower();
// check the file extension is valid or not
if
(allowedExtensions.Contains(ext))
{
// replace name for uniquness and add ext
//this below line generate new name of file
string
name = upload.FileName.ToLower();
// this line is for make path to save in databaseget un
string
rootpath =
"/StudentPhotos/"
;
//in ur case it makes like /ProfileImages/Logo.jpg
//this below code is save file in folder
// this line makes ABSOLUTE path like E://someFolder/someOtherFolders/filename.ext
// because file is saved into physical address
string
s = System.IO.Path.Combine(System.Web.HttpContext.Current.Server.MapPath(rootpath), name);
// finally this line copy paste the image to s path
upload.SaveAs(s);
student.Student_photo =
"~/StudentPhotos/"
+ name;
}
}
}
}
else
{
ModelState.AddModelError(
""
, errorMessage:
"you cannot add file having size more than 40 MB "
);
return
View();
}
Reply
Answers (
3
)
Swagger not working when is deployed
how to get tokens