c# - 使用 asp.net mvc 3 将文件上传到文件夹时访问被拒绝?

标签 c# asp.net asp.net-mvc asp.net-mvc-3 file-upload

我目前正在研究 asp.net mvc 3,现在我正在尝试实现一个用户可以将文件上传到文件夹的应用程序:

这是我的第一个实现,它实际上工作正常,这是 Controller 代码:

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

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

    }
}

最佳答案

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

关于c# - 使用 asp.net mvc 3 将文件上传到文件夹时访问被拒绝?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15092893/

相关文章:

asp.net - 使用 Html.EditorFor 生成具有特定行数和列数的 textarea

c# - Jquery ui 对话框触发多个对话框问题

javascript - 如何从模型创建数组?

c# - 使用 .NET Core 进行 Hangfire 依赖注入(inject)

c# - Dictionary.ContainsKey 总是返回 False

asp.net - 页面加载时的 Ajax 操作链接

c# - 为什么自定义配置部分的 StringValidator 总是失败?

asp.net-mvc - IIS 7.5。无法禁用 URL 重写规则(我删除了规则,但它们仍然影响)

c# - 现有连接被远程主机 web api 强行关闭

c# - C#/Unity3D中的分段线性整数曲线插值