Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8665721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:33:13+00:00 2026-06-12T17:33:13+00:00

I am trying to upload files using AJAX to ASP.NET. I have this Javascript:

  • 0

I am trying to upload files using AJAX to ASP.NET. I have this Javascript:

var xhr = new XMLHttpRequest();

for (var i = 0; i < files.length; i++) {
   xhr.open('post', '/File/Upload', true);
   xhr.setRequestHeader("Content-Type", "multipart/form-data");
   var formData = new FormData();
   formData.append("_file", files[i]);
   xhr.send(files[i]);
}

files is an Array()

Then I try to access the post file in C# code, but the value is always null. How can I resolve this issue?

// Method 1, Result: file = null
HttpPostedFileBase file = Request.Files["_file"];

// Method 2, Result: postedFile.Count = 0
HttpFileCollectionBase postedFile = Request.Files;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:33:15+00:00Added an answer on June 12, 2026 at 5:33 pm

    Assuming you have the following form containing the file input field:

    <form action="/home/index" method="post" enctype="multipart/form-data" onsubmit="return handleSubmit(this);">
        <input type="file" id="_file" name="_file" multiple="multiple" />
        <button type="submit">OK</button>
    </form>
    

    you could try the following function:

    function handleSubmit(form) {
        if (!FormData) {
            alert('Sorry, your browser doesn\'t support the File API => falling back to normal form submit');
            return true;
        }
    
        var fd = new FormData();
        var file = document.getElementById('_file');
        for (var i = 0; i < file.files.length; i++) {
            fd.append('_file', file.files[i]);
        }
    
        var xhr = new XMLHttpRequest();
        xhr.open(form.method, form.action, true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                alert(xhr.responseText);
            }
        };
        xhr.send(fd);
    
        return false;
    }
    

    Now on the server you should be able to retrieve the file using Request.Files.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to upload files using Google Gears and ASP.NET... I assume you can as
I am trying to upload files to a web server using System.Net.WebClient.UploadFile but I
I have a asp.net website which is supposed to upload files to a handler
I am trying to upload a file using jQuery ajax, but keep getting this
I am trying to upload files via FTP using the following script. The file
I'm trying to upload files to the blobstore in my Google App without using
I am using CakePHP 2.1 and trying to upload files into server. I can
I am using railwayjs and I am trying to upload files. I am doing
I am using cakephp 2.1 and i am trying to upload files and how
I'm trying to upload pdf files in the server. And i;m using the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.