2

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

4

1 回答 1

3

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

于 2013-09-16T12:37:35.573 回答