dsb
dsb

Reputation: 2527

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

Upvotes: 7

Views: 10677

Answers (7)

dsb
dsb

Reputation: 2527

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.

Upvotes: 8

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

Upvotes: 1

davidhood2
davidhood2

Reputation: 1367

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

Upvotes: 0

Vigneshwaran Markandan
Vigneshwaran Markandan

Reputation: 945

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.

Upvotes: 3

juvchan
juvchan

Reputation: 6255

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));

Upvotes: 0

Matt Watson
Matt Watson

Reputation: 1010

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"

Upvotes: 0

Antoine Pelletier
Antoine Pelletier

Reputation: 3316

Maybe this :

System.Web.Hosting.HostingEnvironment.MapPath("~\\App_Data")+"\\Images\\"  + filename )

Upvotes: 0

Related Questions