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 6340503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:50:53+00:00 2026-05-24T19:50:53+00:00

I came across this plain js ajax upload code (it seems that jquery $.post

  • 0

I came across this plain js ajax upload code (it seems that jquery $.post does not work with HTML5 for some reason),

    /* If you want to upload only a file along with arbitary data that
       is not in the form, use this */
    var fd = new FormData();
    fd.append("file", document.getElementById('file').files[0]);

    /* If you want to simply post the entire form, use this */
    //var fd = document.getElementById('form1').getFormData();

    var xhr = new XMLHttpRequest();        
    xhr.upload.addEventListener("progress", uploadProgress, false);
    xhr.addEventListener("load", uploadComplete, false);
    xhr.addEventListener("error", uploadFailed, false);
    xhr.addEventListener("abort", uploadCanceled, false);
    xhr.open("POST", "Upload.php");
    xhr.send(fd);

But I have two issues here,

  1. what does this line mean after the object FormData?

fd.append("file", document.getElementById('file').files[0]);

Why do I need an ID there? Can I do something use jquery append() with $('input[type=file]')?

  1. This ajax is only for single file upload, how can I change it for multiple files upload?
  • 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-05-24T19:50:54+00:00Added an answer on May 24, 2026 at 7:50 pm

    what does this line mean after the object FormData?

    fd.append("file", document.getElementById('file').files[0]);
    

    The document.getElementById('file') gets the <input type="file" id="file"> element by its ID. The element.files[0] grabs the first selected file from the element. The fd.append("file", file) appends it to an FormData instance with the field name of file. The fd is later to be sent as multipart/form-data XHR request body.


    Why do I need an ID there? Can I do something use jquery append() with $('input[type=file]')?

    The ID is not necessary. It’s after all just an example in order to be able to get the <input type="file"> from the document by its ID. Note that the append() function in this example is part of the FormData API and is not to be confused with jQuery’s append() function. Also note that the 1st argument of append() represents the field name, not the ID. That they are the same is just a coincidence.


    This ajax is only for single file upload, how can I change it for multiple files upload?

    Just append multiple fields to FormData. Assuming that you have a File[], here’s an example:

    for (var i = 0; i < files.length; i++) { 
        fd.append("file" + i, files[i]);
    }
    

    It’ll be available in the server end by the field names file0, file1, etc.

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

Sidebar

Related Questions

After searching for a tableless model example I came across this code which seems
I came across this snippet of code on MSDN: entityBuilder.Metadata = @res://*/AdventureWorksModel.csdl| res://*/AdventureWorksModel.ssdl| res://*/AdventureWorksModel.msl;
Just came across this quote in a book on OOP that I'm reading, A
I came across this code today AsyncInvoke(OnTimeMessageTimer, (object)null, (ElapsedEventArgs)null); Is there anything wrong with
I came across this piece of code on a website. main(i) { gets(&i); puts();
I came across this code in a large codebase DWORD WINAPI ThreadFunc (LPVOID lpParam)
Came across this conditional in some uncommented Objective-C code: if (w & (w -
I came across this program and its not behaving in expected way. public class
I came across this line of code: if( -f <filename> ) { ... }
I came across this in the Python interpreter source code. void PyThread_delete_key_value(int key) {

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.