0

I have done a deep research trying to find the more pratical way to upload files from the client-side and I decided to go for the input type file html element.

Problem is: I get nothing in the controller and I am not able to see even the fakepath string been set in firebug.

View:

<form action="action" id="id" method="POST" enctype="multipart/form-data">
    <table id="tblId">            
        <tr>
            <td><input type="file" name="file" /></td>
        </tr>
    </table>
    <input type="submit" value="import" />
</form>   

As you can see, I am properly using enctype. Data is been sent to the controller as it should except that there is no data.

Controller:

[HttpPost]
public ActionResult opImportFile(FormCollection form) {


    var file = Request.Files["file"];
        if (file != null)
        {
            return Content("ok");
        }
        else
        {
            return Content("bad");
        }
}

And I am getting always "bad"! Too bad. Is there any other way I could try or am I doing somenthing wrong?

P.S -> It is a ajax request:

$('#formUpdStoringSettings').submit(function (e) {

    e.preventDefault();    
    var form = $(this);       
    alert($('input[name=file]').val()); //Here I am able to get the fakepath...
    alert(form.serialize());  //Here I get nothing...

    $.ajax({
        url: form.attr('action'),
        type: form.attr('method'),
        data: form.serialize(),
        success: function (response) {
        }
   });
}
Guilherme Longo
  • 2,278
  • 7
  • 44
  • 64

1 Answers1

1

You probably will need to use a jQuery upload plug-in to be able to upload using AJAX.

Andy T
  • 10,223
  • 5
  • 53
  • 95
  • I am trying uploadify. Just modified upload.php to upload.cs but it still not working. HTTP ERROR(404). Discussion: http://stackoverflow.com/questions/14609590/http-error-404-with-uploadify-and-custom-uploader-for-asp-net-asp-net-mvc-4 – Guilherme Longo Jan 30 '13 at 17:06