locked
Uploading a file using MVC 3

    Question

  • Hi.

    I'm learning MVC. I really like a lot of it. Some things - to me - are quite different, such as uploading files to a site folder. I have a site with a form. The user is allowed to fill in fields and upload a file. The file that is uploaded should go into the ~/images/  folder in the site. The information should go into the database.

    So, basically what I'm trying to do is allow the user to fill out a form and upload an image. The image will go to the folder in the site, and the path to that image along with other information will be stored in the database.

    Here is what I've tried:

    Controler:

     [HttpPost]
           
    public ActionResult Create(Paintings paintings, HttpPostedFileBase file)
           
    {

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

           
    public ActionResult Create()
           
    {
               
    return View();
           
    }

    View:

     

    @model site.Models.Paintings
             

    @{
        ViewBag.Title = "Create";
    }

    <h2>Create</h2>

    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

    @*@using (Html.BeginForm("Uploadfile", "Containers", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
           
    <input type='file' name='file' id='file' />
           
    <input type="submit" value="submit" />
        }
    *@

    @using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.ValidationSummary(true)
       
    <fieldset>
           
    <legend>Paintings</legend>

           
    <div class="editor-label">
                @Html.LabelFor(model => model.Path)
           
    </div>
           
    <div class="editor-field">
                @Html.EditorFor(model => model.Path)
                @Html.ValidationMessageFor(model => model.Path)
           
    </div>

           
    <div class="editor-label">
                @Html.LabelFor(model => model.Description)
           
    </div>
           
    <div class="editor-field">
                @Html.EditorFor(model => model.Description)
                @Html.ValidationMessageFor(model => model.Description)
           
    </div>

           
    <div class="editor-label">
                @Html.LabelFor(model => model.Size)
           
    </div>
           
    <div class="editor-field">
                @Html.EditorFor(model => model.Size)
                @Html.ValidationMessageFor(model => model.Size)
           
    </div>

           
    <div class="editor-label">
                @Html.LabelFor(model => model.Medium)
           
    </div>
           
    <div class="editor-field">
                @Html.EditorFor(model => model.Medium)
                @Html.ValidationMessageFor(model => model.Medium)
           
    </div>

           
    <div class="editor-label">
                @Html.LabelFor(model => model.Category)
           
    </div>
           
    <div class="editor-field">
                @Html.EditorFor(model => model.Category)
                @Html.ValidationMessageFor(model => model.Category)
           
    </div>

           
    <div class="editor-label">
                @Html.LabelFor(model => model.Title)
           
    </div>
           
    <div class="editor-field">
                @Html.EditorFor(model => model.Title)
                @Html.ValidationMessageFor(model => model.Title)
           
    </div>

           
    <div class="editor-label">
                @Html.LabelFor(model => model.ThumbPath)
           
    </div>
           
    <div class="editor-field">
                @Html.EditorFor(model => model.ThumbPath)
                @Html.ValidationMessageFor(model => model.ThumbPath)
           
    </div>

           
    <input type="file" name="file" id="file" />
           
    <p>
               
    <input type="submit" value="Create" />
           
    </p>
       
    </fieldset>
    }

    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>

     Any Help / Guidance would be greatly appreciated!

    Thanks,

    Thursday, January 05, 2012 8:08 PM

All replies

  • Hi Iron-Man,

    this is not correct forum for answer. you should ask your question in below forum.

    http://forums.asp.net/1146.aspx/1?MVC

     

    Regards

    Rozy

     

    Thursday, January 26, 2012 6:40 PM
  • for fileupload use normal html file control
    Friday, February 03, 2012 10:09 AM
  • Hello,

    Check out the links below. It will help you

    http://stackoverflow.com/questions/5193842/file-upload-asp-net-mvc3-0

    http://stackoverflow.com/questions/4784225/mvc-3-file-upload-and-model-binding

    Saturday, February 04, 2012 9:10 AM