0

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

1
  • @Çağrı Backend get test=bla but doesn't get fff at all Commented Apr 11, 2020 at 17:22

1 Answer 1

1

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

5 Comments

You r right but it's just a typo in the question. Not the real problem. Thank you tho!
this ajax code is right. it works then problem is in your backend. so u should share also backend code too
backend is -- Debug.WriteLine(Request.Form.AllKeys); OR: Request.Form.Count And I'm getting only one key.
I mean are u using .net core or .net mvc ? can u share your code that take request from ajax?
I have updated my question. Yes I was looking in a wrong place :( Thank you. Your question helped me think in a right direction.

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.