c# - 上传的文件未显示在项目解决方案 (Mvc5) 中

标签 c# asp.net-mvc asp.net-mvc-5

我已经实现了以下代码来上传文件。该文件已上传到位置 ("../App_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");
    }

最佳答案

Why is the file not showing up?

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

关于c# - 上传的文件未显示在项目解决方案 (Mvc5) 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25250627/

相关文章:

ASP.NET MVC Bootstrap 无法正常工作

c# - 上传后和将其保存到数据库之前调整图像大小

c# - 集合编辑器数据在设计时丢失

c# - 理解一行示例代码

javascript - 点网,一些 View 无法访问 css&js 文件

c# - 将站点的手动版本号添加到 web.config

javascript - 当下拉列表中的项目发生更改时填充表格 razor mvc3

c# - 为什么我在调用 RoleEnvironment.GetConfigurationSettingValue ("MYKEY"时收到 SEHException?

C# 服务器-客户端应用程序(一服务器-多客户端)

javascript - 使用 C# 从 DatePicker 获取周 ID