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