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

Kevin Andrid
  • 1,963
  • 1
  • 15
  • 26
dsb
  • 2,347
  • 5
  • 26
  • 43

7 Answers7

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.

dsb
  • 2,347
  • 5
  • 26
  • 43
  • 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*. – Anis Alibegić Sep 01 '17 at 11:09
  • 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. – Daniel Boyer Jul 26 '19 at 16:48
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.

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

0

Maybe this :

System.Web.Hosting.HostingEnvironment.MapPath("~\\App_Data")+"\\Images\\"  + filename )
Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62
  • 1
    Thank you for your help. It actually gives me exactly the same result as 'Server.MapPath' and the problem remains. – dsb Nov 18 '15 at 14:54
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"
Matt Watson
  • 980
  • 7
  • 10
  • 1
    Thank you. It still get the same result as 'Server.MapPath' and the problem remains. – dsb Nov 18 '15 at 15:14
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));
juvchan
  • 6,113
  • 2
  • 22
  • 35
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

Community
  • 1
  • 1
davidhood2
  • 1,367
  • 17
  • 47