1

我目前正在学习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\Uploads\'被拒绝。” 这是处理上传的实用程序类:

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

    }
}
4

4 回答 4

3

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

于 2013-02-26T15:31:50.787 回答
2

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

于 2013-02-26T15:37:49.497 回答
0

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

于 2013-12-05T18:49:23.643 回答
0

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++

于 2013-02-26T16:29:56.737 回答