2013-02-26 65 views
0

我正在学习asp.net mvc 3,现在我试图实现一个应用程序,用户可以将文件上传到一个文件夹:使用asp.net mvc 3将文件上传到文件夹时访问被拒绝?

这是我的第一个实现,它实际上工作细,这里是控制器代码:

public class FileUploadController : Controller 
    { 
     // 
     // GET: /FileUpload/ 

     public ActionResult Index() 
     { 
      return View(); 
     } 
     [HttpPost] 
     [ActionName("Upload")] 
     public ActionResult Index(FormCollection form) 
     { 

      string upFolder = Server.MapPath("~/FileUploadFiles/"); 

      if(!Directory.Exists(upFolder)) 
      { 
       Directory.CreateDirectory(upFolder); 
      } 
      HttpPostedFileBase photo = Request.Files["fileupload"]; 

      if (photo != null) 
      { 
       photo.SaveAs(upFolder+photo.FileName); 
       return RedirectToAction("Index"); 
      } 



      return View(); 
     } 

    } 

这里是我有另一种实现方式,我得到一个错误“访问路径‘UserUploads \上传\’被拒绝。”这里是处理上载的utitility类:

public static class FileUploader 
    { 
     public static char DirSeparator = Path.DirectorySeparatorChar; 
     public static string FilesPath = "UserUploads" + DirSeparator + "Uploads" + DirSeparator; 

     public static string UploadFile(HttpPostedFileBase file) 
     { 
      //check if we have a file 
      if(file == null) 
      { 
       return ""; 
      } 

      //make sure the file has content 
      if(!(file.ContentLength > 0)) 
      { 
       return ""; 
      } 

      string fileName = file.FileName; 
      string fileExt = Path.GetExtension(file.FileName); 

      //make sure we are able to determine a proper extension 
      if(fileExt == null) 
      { 
       return ""; 
      } 

      //check if directory does not exists 
      if(!Directory.Exists(FilesPath)) 
      { 
       Directory.CreateDirectory(FilesPath); 
      } 

      //set our full path for saving 
      string path = FilesPath + DirSeparator + fileName; 
      //Save the file 
      file.SaveAs(Path.GetFullPath(path)); 

      //Return the filename 
      return fileName; 

     } 

     public static void DeleteFile(string fileName) 
     { 
      //Don't do anything if there is no name 
      if(fileName.Length > 0) 
      { 
       return; 
      } 

      //Set our full path for deleting 
      string path = FilesPath + DirSeparator + fileName; 

      //Check if our file exists 
      if(File.Exists(Path.GetFullPath(path))) 
      { 
       File.Delete(Path.GetFullPath(path)); 
      } 
     } 

这里是控制器代码:

using MvcFileUpload.Utility; 

namespace MvcFileUpload.Controllers 
{ 
    public class UploadFilesController : Controller 
    { 
     // 
     // GET: /UploadFiles/ 

     public ActionResult Index() 
     { 
      return View(); 
     } 

     [HttpPost] 
     [ActionName("Upload")] 
     public ActionResult Index(HttpPostedFileBase file) 
     { 

      FileUploader.UploadFile(file); 
      return RedirectToAction("Index"); 
     } 

    } 
} 
+3

这不是一个答案,但你应该看看静态方法'System.IO.Path.Combine()'。它使得使用路径更容易。它会为您处理目录分隔符,并且会很好地处理相对路径。 – 2013-02-26 15:33:51

+0

谢谢你的提示。 – 2013-02-26 15:37:14

回答

0

我能够

通过修改实用程序类来解决这个问题:

public static string FilesPath = HttpContext.Current.Server.MapPath("~\\UserUploads" + DirSeparator + "Uploads" + DirSeparator); 

谢谢您先生/女士提供的解决方案。谢谢++

+0

目录分隔符只是一个字符串DirSeparator =“\\”'您定义的或类/库的一部分吗? – Chopo87 2013-06-12 14:55:34

+0

没关系,我看到你在上面的类中定义了它,抱歉'public static char DirSeparator = Path.DirectorySeparatorChar' – Chopo87 2013-06-12 14:57:30

3

哪里是要创建的假设目录FilePath?网站根文件夹内?您应该手动创建“UserUploads”文件夹,并为其提供AppPool(包括您的Web应用程序)在其下运行写入权限的帐户。

+0

是的,“UserUploads”它应该在网站根文件夹中创建,但是我创建的代码实际上创建了该目录(如果它不存在)。 – 2013-02-26 15:38:26

+2

要能够在网站文件夹内创建“UserUploads”,AppPool身份必须具有网站文件夹的“写入”权限。这是违反安全考虑。您应该指定将文件存储为“〜/ UserUploads”和“MapPath”到文件夹的位置。这样,您可以将“UserUploads”添加为网站的虚拟目录,并将其物理位置设置在其他位置。 – Igor 2013-02-26 15:49:29

+0

@Igor我们怎样才能做到物理文件夹? – Antoops 2016-12-08 06:26:42

2

有没有可能路径不正确?我注意到,在实现中,您使用Server.MapPath()为目录使用完整的物理路径,但实用程序类只有部分路径。如果您尝试将完整路径分配给您的FilesPath变量,会发生什么情况?如果您仍然遇到问题,建议您运行ProcMon以获取有关生成访问被拒绝错误时文件系统上发生的更多信息。

0

没有解决方案帮助我。我正在考虑将用户分配到具有目录访问权限的IIS。

相关问题