Reputation: 55032
I m trying to upload an image and a thumbnail.
I have set the upload path in web.config as <add key="UploadPath" value="/Images"/>
when i upload the image, it get the full path of the hard drive and folder that application is in|:
D:\Projects\Social\FooApp\FooApp.BackOffice\Images\image_n.jpg
But i just want /images/image_n.jpg
I m using Path.Combine
do u think that the reason?
how can i resolve this?
this is the code|:\
foreach (var file in files)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
if (fileName != null) originalFile = Server.MapPath(upload_path) + DateTime.Now.Ticks + "_ " + fileName;
file.SaveAs(originalFile);
images.Add(originalFile);
}
}
Upvotes: 2
Views: 2656
Reputation: 1656
You need to use HttpContext.Current.Server.MapPath.
Returns the physical file path that corresponds to the specified virtual path on the Web server.
Your code can look something like this:
Path.Combine(HttpContext.Current.Server.MapPath("~/Images"), fileName);
*EDIT - I'm adding to the code you've provided above. It would look something like this.
foreach (var file in files)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var uploadPath = "~/Images"; //This is where you would grab from the Web.Config. Make sure to add the ~
if (fileName != null) {
var originalFile = Path.Combine(HttpContext.Current.Server.MapPath(uploadPath), DateTime.Now.Ticks + "_ " + fileName);
file.SaveAs(originalFile);
images.Add(originalFile);
}
}
}
Upvotes: 6
Reputation: 6111
I assume this code is in one of your controllers. Have you tried:
Server.MapPath(yourPath);
Upvotes: 0