shlasasha
shlasasha

Reputation: 167

AJAX upload file

I know there is a ton of questions like this but I can't get things to work :( In script below only "test=bla" is posted and file is not. What am I doing wrong? Thanks!!!

<input type="file" name="fileUpload" id="fileUpload" onchange="fileUploadChange(this);"   />


function fileUploadChange(e) {
    var mydata = new FormData();
    mydata.append("test", "bla");
    mydata.append("fff", e.files[0]);

    $.ajax({
        type: "POST",
        url: "TestApp/Home/fileUpload",
        contentType: false,
        processData: false,
        data: mydata,
        success: function (data) {
            alert("success");
        }
        error: function (error) {
            alert("error");
        }
    });
}

After further investigation I have figured it out: in .NET MVC, Controller.Request object has Form property and Files property. Simple data items like strings, integers etc can be found in the Form.Keys, Files are located in Request.Files

Upvotes: 0

Views: 47

Answers (1)

Cagri Tacyildiz
Cagri Tacyildiz

Reputation: 17570

u missed , between success and error methods.

$.ajax({
        type: "POST",
        url: "TestApp/Home/fileUpload",
        contentType: false,
        processData: false,
        data: mydata,
        success: function (data) {
            alert("success");
        },
        error: function (error) {
            alert("error");
        }
    });

Upvotes: 1

Related Questions