Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
File not sending with FormData in Ajax request

I have an ajax request that is sending a file along with some other info. Everything except the file is the correct value, and the file is always null. I can't figure out why.

Here is the input I have included in the form:

<input type="file" id="file" runat="server"/>

On button click, it calls this function:

function ButtonClick() {
    var file = document.getElementById('file').files[0]; // correctly gets the file

    var formData = new FormData();
    formData.append('key1', 'val1');
    formData.append('key2', 'val2');
    formData.append('file', file, file.name);

    $.ajax({
        cache: false,
        data: formData,
        dataType: 'json',
        processData: false,
        contentType: false,
        success: function (data, textStatus, jqXHR) { closeWindow(); },
        timeout: 30000,
        type: 'POST',
        url: '<%= ResolveClientUrl("~/api/Example/ExampleUrl") %>'
    });
}

When I receive the request server-side, I get the following values:

var key1= HttpContext.Current.Request.Form["key1"]); // returns "val1"
var key2= HttpContext.Current.Request.Form["key2"]); // returns "val2"
var file= HttpContext.Current.Request.Form["file"]); // returns "null"

And when I take a look at HttpContext.Current.Request.Form, "file" is not in the request at all.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The file should be accessible via

HttpContext.Current.Request.Files["file"]
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!