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.

3
  • Check stackoverflow.com/questions/17128196/… Commented Jul 2, 2014 at 8:09
  • possible duplicate of File Upload ASP.NET MVC 3.0 Commented Jul 2, 2014 at 8: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" .... Commented Jul 2, 2014 at 8:37

4 Answers 4

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);
 }
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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

1 Comment

This may not have answered the question, but it helped me. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.