Uploading in MVC Dynamically creating folder with persmissions

Don Thomas Boyle picture Don Thomas Boyle · Sep 11, 2013 · Viewed 10.1k times · Source

VS'12 Ineternet Application Template asp.net C# MVC4, EF Code First

Upload Post Method

if (ModelState.IsValid)
        {        
                foreach (var file in attachments)
                {
                    string strMappath = "~/UploadedImages/" +var1+ "/" + var2+ "/" + var3+ "/" + var4+ "/" + var5 + "/";

                    if (!Directory.Exists(strMappath))
                    {
                        DirectoryInfo di = Directory.CreateDirectory(strMappath);
                    }

                    // Some browsers send file names with full path. We only care about the file name.
                    //var fileName = Path.GetFileName(file.FileName);
                    var fileName = Path.GetFileNameWithoutExtension(file.FileName) + Path.GetExtension(file.FileName);

                    var destinationPath = Path.Combine(
                        Server.MapPath(strMappath), fileName);
                    file.SaveAs(destinationPath);
                }

My Questions are

  1. Why is it not creating my Folders
  2. When i ( cheat and make them myself ) i have no permissions...
  3. How can you best upload Dynamically ( I would like to keep uploaded files separately )
  4. Is there a better way to manage them?

This is for both on the Dev machine though IIS and on my Server-also IIS

Answer

Pakk picture Pakk · Sep 16, 2013

Use Server.MapPath(strMappath) to create folders and check if exist.