0

What is wrong with this:

View

@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" />
}

Controller:

[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);
}

I can write the file, but the file name is not saved to database, I'm not getting errors, I can update TimeZone field (and so many others).

What I'm doing wrong? I cannot figure out!!!

Thanks

Santiago
  • 2,190
  • 10
  • 30
  • 59
  • Not the answer but please see this:- http://stackoverflow.com/questions/4535627/where-is-the-best-place-to-save-images-from-users-upload/4535684#4535684 and if you care about security stop using the file name the user gave you for the file that you save it to. – Ian Mercer Jun 11 '11 at 04:46
  • Thank you, I'll consider that risks, but that page don't solve my problem. – Santiago Jun 11 '11 at 15:45

3 Answers3

1

You should generate a new and valid filename by yourself. It will be less error-prone for your issue. And it will avoid security issues.

For example, you can generate the filename using a string.Format():

string myGeneratedFileName = string.Format("client-logo-{0}", Guid.NewGuid()); string filePath = Path.Combine(HttpContext.Server.MapPath("/Upload/"), myGeneratedFileName); file.SaveAs(filePath); client.Logo = myGeneratedFileName;

And BTW, you may want to handle exceptions on file.SaveAs().

SandRock
  • 5,276
  • 3
  • 30
  • 49
  • Thanks, but my problem is to save the name to the database. If I use `client.Logo = "myfile";` doesn't work also. – Santiago Jun 12 '11 at 00:31
1

If only the name is not being saved, then perhaps you have an issue with the data model. Can you check the Logo property is mapped to a valid column? You can try to remove and re-add the table in the designer to update mapping information.

SandRock
  • 5,276
  • 3
  • 30
  • 49
  • I found the problem, the `client.Logo = "blabla"` must be after `UpdateModel` method, I don't know why, because if I debug the code the `client.Logo` value I setted is not erased after execution of `UpdateModel` – Santiago Jun 12 '11 at 14:41
  • Then you shall add an answer to your own question and mark it as valid :) – SandRock Jun 12 '11 at 19:19
1

I found the problem, the client.Logo = "blabla" must be after UpdateModel method, I don't know why, because if I debug the code the client.Logo value I setted is not erased after execution of UpdateModel, like these:

[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);
}
Santiago
  • 2,190
  • 10
  • 30
  • 59
  • Absolutely spot on this! I had a similar issue with uploading the file it was uploading it to the directory and then creating the database record but not adding the file extension. – Crouch Jan 17 '14 at 12:16