0

I'm planning to add file upload using ajax and form submission in same view

@model project_name.Models.ProductFinanceFeatures

@{
 }

<h2>--------File_Upload-------------</h2>

@using (Ajax.BeginForm("Financing_Product_Feature_Upload", "FileUpload", new { enctype = "multipart/form-data" }, new AjaxOptions() { HttpMethod = "POST" }))
{
    @Html.AntiForgeryToken()

    <input type="file" name="files">
    <input type="submit" value="Upload File to Server">
}    

<h2>-------Form_Submission-----</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()   

    <div class="form-horizontal">          
          ......                  
    </div>
}

this is my controller class method for ajax

    [HttpPost]
    public ActionResult Financing_Product_Feature_Upload(HttpPostedFileBase files)
    {
      ...
    }

once I select a file and click "upload" button ,then I debug this I can see files value become null

kez
  • 2,273
  • 9
  • 64
  • 123
  • You cannot use `Ajax.BeginForm()` to upload files. You need to use `FormData` Refer [this answer](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) for one way –  Mar 29 '16 at 07:11
  • Actually its bit hard for me to understand what should include , view and controller classes , as I understood , ajax postback function should be under section script of viewpage , other things bit unclear for me , – kez Mar 29 '16 at 07:19
  • You don't need to include anything else. Read my first comment - you **CANNOT** use `Ajax.BeginForm()` to upload a file –  Mar 29 '16 at 07:20
  • what should I remove on my view then ? how to trigger this ajax function ? – kez Mar 29 '16 at 07:25
  • You can just use a `
    ` tag with the file input and a button and handle the button's click event to call the `$.ajax()` method
    –  Mar 29 '16 at 07:29
  • @StephenMuecke I created as you guided , but once I debug it cannot direct to `Financing_Product_Feature_Upload` here the [code](https://bitbucket.org/snippets/Common_Admin/prg9G) – kez Mar 29 '16 at 08:39
  • First you need to use a ` –  Mar 29 '16 at 08:46
  • where should I include this `var formdata = new FormData($('#fileform').get(0));` ? inside controller or under ajax function ? – kez Mar 29 '16 at 09:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107610/discussion-between-stephen-muecke-and-kez). –  Mar 29 '16 at 09:00
  • @StephenMuecke now seems like okay inside the method I have a conditon like following `if (Request.Files["file"].ContentLength > 0){..` when I debug I can see file is transporting , but when It comes to above condition it cannot give me `true` or `false` – kez Mar 29 '16 at 09:18

0 Answers0