2011-06-11 40 views
0

什麼是錯的: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

回答

1

我發現問題,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

1

你應該自己生成一個新的有效文件名。對於您的問題,這將不太容易出錯。它會避免安全問題。

例如,可以使用的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

1

如果只有名稱未被保存,那麼可能是數據模型存在問題。你可以檢查Logo屬性映射到一個有效的列嗎?您可以嘗試刪除並重新添加設計器中的表以更新映射信息。

+0

我發現這個問題,'client.Logo ='blabla''必須在'UpdateModel'方法後面,我不知道爲什麼,因爲如果我調試代碼,我設置的'client.Logo'值不會被擦除執行'UpdateModel' – Santiago 2011-06-12 14:41:24

+0

然後你應該爲你自己的問題添加一個答案,並將其標記爲有效:) – SandRock 2011-06-12 19:19:57

相關問題