C# 上载的文件不会显示在项目解决方案(Mvc5)中

C# 上载的文件不会显示在项目解决方案(Mvc5)中,c#,asp.net-mvc,asp.net-mvc-5,C#,Asp.net Mvc,Asp.net Mvc 5,我已经实现了下面的代码来上传一个文件。该文件已上载到位置(“../App\u Data/uploads”),但未显示在项目中。我必须在项目中手动包含该文件。为什么文件没有显示 public ActionResult ChangeSetting(SettingViewModel setting) { string userId = User.Identity.GetUserId(); ApplicationUser currentUser = this._

我已经实现了下面的代码来上传一个文件。该文件已上载到位置(“../App\u Data/uploads”),但未显示在项目中。我必须在项目中手动包含该文件。为什么文件没有显示

 public ActionResult ChangeSetting(SettingViewModel setting)
    {
        string userId = User.Identity.GetUserId();
        ApplicationUser currentUser = this._appUserRepo.Find(e => e.Id.Equals(userId)).FirstOrDefault();

        if (setting.PictureUrl != null)
        {
            if (setting.PictureUrl.ContentLength > 0)
            {
                string fileName = Path.GetFileName(setting.PictureUrl.FileName);
                if (fileName != null)
                {

                    string path = Path.Combine(Server.MapPath("../App_Data/uploads"), fileName);
                    setting.PictureUrl.SaveAs(path);
                    if (currentUser != null)
                    {
                        currentUser.PictureUrl = path;
                        currentUser.PictureSmalUrl = path;
                        currentUser.PictureBigUrl = path;
                    }
                }
            }
        }
        if (setting.FirstName != null)
        {
            if (currentUser != null) currentUser.FirstName = setting.FirstName;
        }           
        _appUserRepo.Update(currentUser);
        return RedirectToAction("index", "Admin");
    }
为什么文件没有显示

 public ActionResult ChangeSetting(SettingViewModel setting)
    {
        string userId = User.Identity.GetUserId();
        ApplicationUser currentUser = this._appUserRepo.Find(e => e.Id.Equals(userId)).FirstOrDefault();

        if (setting.PictureUrl != null)
        {
            if (setting.PictureUrl.ContentLength > 0)
            {
                string fileName = Path.GetFileName(setting.PictureUrl.FileName);
                if (fileName != null)
                {

                    string path = Path.Combine(Server.MapPath("../App_Data/uploads"), fileName);
                    setting.PictureUrl.SaveAs(path);
                    if (currentUser != null)
                    {
                        currentUser.PictureUrl = path;
                        currentUser.PictureSmalUrl = path;
                        currentUser.PictureBigUrl = path;
                    }
                }
            }
        }
        if (setting.FirstName != null)
        {
            if (currentUser != null) currentUser.FirstName = setting.FirstName;
        }           
        _appUserRepo.Update(currentUser);
        return RedirectToAction("index", "Admin");
    }
因为这就是Visual Studio项目的工作方式—只有手动包含到项目中的文件才会显示在解决方案资源管理器中。随后由永久进程动态添加到文件夹中的文件不会显示在Visual Studio中(除非您手动将其包含并使其成为解决方案的一部分)。但这可能不是你应该担心的事情。用户上载的文件不会自动显示在Visual Studio项目中。重要的是它们存在于预期的位置,并且您的web应用程序能够在运行时访问它们。只需在Windows资源管理器中打开文件夹的物理位置,以确认代码是否按预期工作

为什么文件没有显示

 public ActionResult ChangeSetting(SettingViewModel setting)
    {
        string userId = User.Identity.GetUserId();
        ApplicationUser currentUser = this._appUserRepo.Find(e => e.Id.Equals(userId)).FirstOrDefault();

        if (setting.PictureUrl != null)
        {
            if (setting.PictureUrl.ContentLength > 0)
            {
                string fileName = Path.GetFileName(setting.PictureUrl.FileName);
                if (fileName != null)
                {

                    string path = Path.Combine(Server.MapPath("../App_Data/uploads"), fileName);
                    setting.PictureUrl.SaveAs(path);
                    if (currentUser != null)
                    {
                        currentUser.PictureUrl = path;
                        currentUser.PictureSmalUrl = path;
                        currentUser.PictureBigUrl = path;
                    }
                }
            }
        }
        if (setting.FirstName != null)
        {
            if (currentUser != null) currentUser.FirstName = setting.FirstName;
        }           
        _appUserRepo.Update(currentUser);
        return RedirectToAction("index", "Admin");
    }

因为这就是Visual Studio项目的工作方式—只有手动包含到项目中的文件才会显示在解决方案资源管理器中。随后由永久进程动态添加到文件夹中的文件不会显示在Visual Studio中(除非您手动将其包含并使其成为解决方案的一部分)。但这可能不是你应该担心的事情。用户上载的文件不会自动显示在Visual Studio项目中。重要的是它们存在于预期的位置,并且您的web应用程序能够在运行时访问它们。只需在Windows资源管理器中打开文件夹的物理位置,以确认您的代码是否按预期工作。

要添加一个警告:如果您使用“删除额外文件”进行干净发布,则所有用户上载的内容将在生产时被删除。建议将用户上载的文件放在网站外的文件夹中。希望添加一个警告:如果您使用删除额外文件进行干净发布,则所有用户上载的内容将在生产时删除。建议用户上传的文件放在网站外的文件夹中。