0

I want to upload a file in folder image.
I use ASP.NET with MVC4 and razor.

I have this in my View :

    <div class="editor-label">
         @Html.Label("Descriptif")
    </div>
    <div class="editor-field">
         <input type="file" name="fDescriptif" />
         @Html.ValidationMessageFor(model => model.fDescriptif)
    </div>
[...]
    <p>
        <input type="submit" value="Create" />
    </p>

In my controller :

[HttpPost]
public ActionResult Create(formation formation)
{
    if (ModelState.IsValid)
    {
        db.formations.Add(formation);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(formation);
}

In my model :

 public string fDescriptif { get; set; }

I have no idea what I must do to upload my file in my folder "image" and save just the name of my file in my database. When I validate my form it's my complete path who saved.

Cecile.jm
  • 25
  • 2
  • 11
  • Check http://stackoverflow.com/questions/17128196/file-upload-as-part-of-form-with-other-fields/17128313#17128313 – Mate Jul 02 '14 at 08:09
  • possible duplicate of [File Upload ASP.NET MVC 3.0](http://stackoverflow.com/questions/5193842/file-upload-asp-net-mvc-3-0) – Martin Steel Jul 02 '14 at 08:19
  • Your links show how only upload when I need to upload my file, and save its name in my database for data and the rest of the data on my model "formation" .... – Cecile.jm Jul 02 '14 at 08:37

4 Answers4

1

you can find many questions like this, even on SO many answers have been given for this topic.

Here are 2 of the links. you should find your answer in this links. and many more other answers are there for this on SO. just google it, and you will find them.

https://stackoverflow.com/a/5193851/1629650

https://stackoverflow.com/a/15680783/1629650

Community
  • 1
  • 1
Vishal Ravlani
  • 425
  • 3
  • 12
1

On your view make sure you have added enctype = "multipart/form-data" to your form tag, and then try like this:

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmID" }))

And you can Get your file in Controller Action AS:

[HttpPost]
 public ActionResult Create(formation formation,HttpPostedFileBase file)
 {
   if (ModelState.IsValid)
   {
     // Get your File from file
   }

   return View(formation);
 }
Pawan
  • 1,704
  • 1
  • 16
  • 37
0

Your form doesn't contain any input tag other than the file so in your controller action you cannot expect to get anything else than the uploaded file (that's all that's being sent to the server). One way to achieve this would be to include a hidden tag containing the id of the model which will allow you to retrieve it from your datastore inside the controller action you are posting to (use this if the user is not supposed to modify the model but simply attach a file):

@using (Html.BeginForm("ACTION", "CONTROLLER", FormMethod.Post, new { enctype = "multipart/form-data" }))

 @Html.TextBoxFor(m => m.File, new { @type = "file" })

AND IN CONTROLLER

[HttpPost]
    public ActionResult ACTION(ViewModels.Model taskModel)
    {
      string path = @"D:\Projects\TestApps\uploadedDocs\";
         if (taskModel.File != null) {
                taskModel.File.SaveAs(path +taskModel.TaskTitle 
                            + taskModel.File.FileName);
                 newUserProject.DocumentTitle = taskModel.TaskTitle 
                            + taskModel.File.FileName;
                  newUserProject.DocumentPath = path + taskModel.File.FileName;
                    }
}
Shekhar Pankaj
  • 9,065
  • 3
  • 28
  • 46
-1
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
  <input type="file" name="file" id="file" />
  <input type="submit" />
</form>

And your controller should be as below

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {

        if (file.ContentLength > 0)
        {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
            file.SaveAs(path);
        }

        return RedirectToAction("Index");
    }
Rajnikant
  • 2,176
  • 24
  • 23