Server.MapPath does not find the path on Azure
Asked Answered
H

7

7

I have deployed my project to Azure. In my project I have "App_Data\Images" folder.

Now I'm trying to do the following:

String filename = GLOBAL_IMAGES_VALS.GET_FILE_PREFIX(imageType) + "-" + User.Identity.GetUserId<int>().ToString() + Path.GetExtension(image.FileName);

String origPath = Server.MapPath("~\\App_Data")+"\\Images\\"  + filename;

But then upon trying:

image.SaveAs(origPath);

I get this error message:

Could not find a part of the path 'D:\home\site\wwwroot\App_Data\Images\logo-10003065.jpg'.

How can I save my file to "App_Data\Images\"?

Haemic answered 18/11, 2015 at 14:5 Comment(4)
The image loads correctly ? What kind of modification you do to that image before saving it ?Dramaturge
Check if you do have App_Data or Images folder beneath site\wwwroot.Before
@Haemic any resolution on this problem?Southeastwards
@Lumirris Hi. It was a long time ago. The actual problem was that the subfolder 'Images' did not exist. I can't remember why the publish process did not create this sub-folder, so I added it manually and then everything worked fine.Haemic
H
8

The actual problem was that the sub-folder 'Images' did not exist. I can't remember why the publish process did not create this sub-folder, however I added it manually and then everything worked fine.

EDIT:

As others wrote here (@Spectarion). I'll put here the important remark that explain why the folder was not created:

Just for the future readers, folder won't be created if it's empty. Folder is empty even if there are files that are not included in project.

Just put some 'fake.txt' file into any folder you want to make sure that it will be created, and of course don't forget to add it to the project. Good luck.

Haemic answered 19/5, 2016 at 8:49 Comment(2)
Just for the future readers, folder won't be created if it's empty. Folder is empty even if there are files that are not included in project.Interne
This works, something i forgot to do however was actually make sure i could see it in the solution explorer. When I opened it in the file explorer I saw my file but I couldn't see it in my project so I dragged the file from the explorer into my folder in the solution and it worked.Ensample
N
3

Since you don't have any file in the particular folder, while publishing Web deploy ignores the empty folder.

Quick fix: Add any file to the folder before publishing will fix this issue.

Neubauer answered 1/11, 2016 at 18:31 Comment(0)
K
1
if (!Directory.Exists(Server.MapPath("~/Images")))
{                
      Directory.CreateDirectory(Server.MapPath("~/Images"));
}

The directory might be missing in the folder. Create the directory and use it in file path

Kozloski answered 16/10, 2017 at 11:37 Comment(0)
D
0

Maybe this :

System.Web.Hosting.HostingEnvironment.MapPath("~\\App_Data")+"\\Images\\"  + filename )
Dramaturge answered 18/11, 2015 at 14:25 Comment(1)
Thank you for your help. It actually gives me exactly the same result as 'Server.MapPath' and the problem remains.Haemic
C
0

Maybe the images folder doesn't exist and you need to create it first? Although I wouldn't recommend saving images in your app like this if it is designed for people uploading images. I would save them in Azure storage via blobs or the new Azure File storage. I would keep your app deployment files clean just related to your app and save any user generated content outside of it.

BTW, If you are using Azure Web Apps you can use the environment variable of "HOME" to always get the correct path (which should be D:\home)

string path = Environment.GetEnvironmentVariable("HOME") +
                           "\\site\\wwwroot\\App_Data\\images"
Costumier answered 18/11, 2015 at 14:52 Comment(1)
Thank you. It still get the same result as 'Server.MapPath' and the problem remains.Haemic
F
0

I assume your AppData folder is just under the wwwroot folder, which is usually the case.

Try this:

HttpContext.Current.Server.MapPath(Path.Combine("~/AppData/Images/", filename));
Flews answered 20/11, 2015 at 10:31 Comment(0)
D
0

I just had this problem on VS15. I first followed the advice in this question in order to generate the error you've got. I'm guessing this follows in part dsb's answer, but dsb hasnt given any description of the actual process of fixing this.

I then went to https://<mywebsite>.scm.azurewebsites.net/DebugConsole to look through the directory and found that App_Data had not been published

Which was why the error was throwing. So, I then solved this by simply going to the solution explorer, right clicking App_Dataand selecting to "Publish App_Data".

However, my website was a short-term academic effort for a project - I think there is probably a lot to be said for considering Matt Watsons answer above about whether or not allowing users to upload to the deployment area is a good idea

Dogie answered 17/12, 2016 at 20:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.