asp.net MVC 3 - 将文件上传到服务器,我可以将文件名不保存到数据库模型

问题描述:

什么是错的:asp.net MVC 3 - 将文件上传到服务器,我可以将文件名不保存到数据库模型

查看

@model GestionWeb.DAL.Client 
@using (Html.BeginForm("Index", "Config", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    @Html.LabelFor(model => model.TimeZone) 
    @Html.EditorFor(model => model.TimeZone) 
    @Html.ValidationMessageFor(model => model.TimeZone) 

    @Html.HiddenFor(model => model.ClientId) 
    @Html.LabelFor(model => model.Logo) 
    <input type="file" name="Logo" id="Logo" /> 
    @Html.ValidationMessageFor(model => model.Logo) 
    <input type="submit" value="Upload" /> 
} 

控制器:

[HttpPost] 
public ActionResult Index(Client client) 
{ 
    if (ModelState.IsValid) 
    { 
     if (Request.Files[0].ContentLength > 0) 
     { 
      HttpPostedFileBase file = Request.Files[0]; 
      string filePath = Path.Combine(HttpContext.Server.MapPath("/Upload/"), Path.GetFileName(file.FileName)); 
      file.SaveAs(filePath); 
      client.Logo = file.FileName; 
     } 
     db.Client.Attach(client); 
     UpdateModel<Client>(client); 
     //db.ObjectStateManager.ChangeObjectState(client, EntityState.Modified); 
     db.SaveChanges(); 

     return RedirectToAction("Index"); 
    } 
    return View(client); 
} 

我可以写入文件,但文件名不会保存到数据库中,我没有收到错误,我可以更新TimeZone字段(以及其他许多字段)。

我在做什么错了?我无法弄清楚!

谢谢

+0

不是问题的答案,但请看到这一点: - http://stackoverflow.com/questions/4535627/where-is-the-best-place-to-save-images-from-users上传/ 4535684#4535684如果您关心安全停止使用文件名称用户给您的文件,您将其保存到。 – 2011-06-11 04:46:54

+0

谢谢,我会考虑这些风险,但该页面并不能解决我的问题。 – Santiago 2011-06-11 15:45:19

我发现问题,client.Logo =“blabla”必须在UpdateModel方法之后,我不知道为什么,因为如果我调试代码,我设置的client.Logo值在执行UpdateModel后没有被擦除,像这样的:

[HttpPost] 
public ActionResult Index(Client client) 
{ 
    if (ModelState.IsValid) 
    { 
     if (Request.Files[0].ContentLength > 0) 
     { 
      HttpPostedFileBase file = Request.Files[0]; 
      string filePath = Path.Combine(HttpContext.Server.MapPath("/Upload/"), Path.GetFileName(file.FileName)); 
      file.SaveAs(filePath); 
     } 
     db.Client.Attach(client); 
     UpdateModel<Client>(client); 
     **client.Logo = file.FileName;** 
     db.SaveChanges(); 

     return RedirectToAction("Index"); 
    } 
    return View(client); 
} 
+0

绝对是这个!我有一个上传文件的类似问题,它将文件上传到目录,然后创建数据库记录但不添加文件扩展名。 – Crouch 2014-01-17 12:16:15

你应该自己生成一个新的有效文件名。对于您的问题,这将不太容易出错。它会避免安全问题。

例如,可以使用的String.format()生成的文件名:

串myGeneratedFileName =的String.Format( “客户端 - logo- {0}”,Guid.NewGuid()); string filePath = Path.Combine(HttpContext.Server.MapPath(“/ Upload /”),myGeneratedFileName); file.SaveAs(filePath); client.Logo = myGeneratedFileName;

顺便说一句,你可能想要处理例外file.SaveAs()

+0

谢谢,但我的问题是将名称保存到数据库。如果我使用'client.Logo =“myfile”;'也不起作用。 – Santiago 2011-06-12 00:31:26

如果只有名称未被保存,那么可能是数据模型存在问题。你可以检查Logo属性映射到一个有效的列吗?您可以尝试删除并重新添加设计器中的表以更新映射信息。

+0

我发现这个问题,'client.Logo ='blabla''必须在'UpdateModel'方法后面,我不知道为什么,因为如果我调试代码,我设置的'client.Logo'值不会被擦除执行'UpdateModel' – Santiago 2011-06-12 14:41:24

+0

然后你应该为你自己的问题添加一个答案,并将其标记为有效:) – SandRock 2011-06-12 19:19:57