2013-02-26 93 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。

相關問題