Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server.MapPath does not find the path on Azure

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\"?

like image 266
dsb Avatar asked Nov 18 '15 14:11

dsb


People also ask

How do I access wwwroot folder in Azure?

Click on the Go button on the Advanced Tools window as shown below. You can able to see Debug console DropDown on the top of the page, select the CMD option from that dropdown. Click on the Site folder from the below window as highlighted. Here is the wwwroot folder that you want to access using the Kudu advanced tool.

How do I use MapPath?

Specifies the relative or virtual path to map to a physical directory. If Path starts with either a forward (/) or backward slash (\), the MapPath method returns a path as if Path were a full, virtual path. If Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the .

What is server MapPath?

Server. MapPath specifies the relative or virtual path to map to a physical directory. Server.MapPath(".") 1 returns the current physical directory of the file (e.g. aspx) being executed. Server.MapPath("..")


1 Answers

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.

like image 85
dsb Avatar answered Nov 03 '22 01:11

dsb