简体   繁体   中英

Access denied when uploading files to folder using asp.net mvc 3?

I'm currently studying asp.net mvc 3, and right now I'm trying to implement a application where a user can upload a file to a folder:

Here's the first implementation that I have and it's actually working fine, here is the controller code:

   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();
        }

    }

Here is another implementation that I have, I'm getting an error "Access to the path 'UserUploads\\Uploads\\' is denied." Here is the utitility class that handles the upload:

 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));
            }
        }

Here is the code for the controller:

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");
        }

    }
}

Where is the FilePath directory supposed to be created? Inside the website root folder? You should create "UserUploads" folder manually and give the account under which the AppPool (that includes your web application) runs the permission to write there.

Is it possible that the path isn't correct? I notice that in the implementation that works you are using a full physical path for the directory by using Server.MapPath() but the utility class only has a partial path. What happens if you try assigning a full path to your FilesPath variable? If you are still having trouble, I would recommend running ProcMon to get more information about what's happening on the filesystem when the access denied error is generated.

I was able to solve it by

by modifying the utility class:

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

Thank you Sir/Ma'am for providing solutions. Thank you++

none of solution helped me. I am thinking of assigning user to IIS with the directory access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM