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

  • SEARCH
  • Home
  • 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 7532153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:22:10+00:00 2026-05-30T05:22:10+00:00

Can I send a file as multipart by XMLHttpRequest to a servlet? I am

  • 0

Can I send a file as multipart by XMLHttpRequest to a servlet?

I am making a form and submitting it as multipart, but somehow I am not getting a response for successfully uploading it. I do not want the page to be refreshed, so it has to take place by Ajax.

  • 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-30T05:22:11+00:00Added an answer on May 30, 2026 at 5:22 am

    That’s only possible with the XHR FormData API (previously known being part of as "XHR2" or "XHR Level 2", currently known as "XHR Advanced Features").

    Given this HTML,

    <input type="file" id="myFileField" name="myFile" />
    

    you can upload it as below:

    var formData = new FormData();
    formData.append("myFile", document.getElementById("myFileField").files[0]);
    
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "myServletUrl");
    xhr.send(formData);
    

    XHR will take care about proper headers and request body encoding and the file will in this example be available on the server side as form-data part with the name myFile.

    You need to keep in mind that FormData API is not supported in older browsers. At caniuse.com you can see that it’s currently implemented in Chrome 7+, Firefox 3.5+, Safari 5+, Internet Explorer 10+ and Opera 12+.

    In case you’re using jQuery, then you might be tempted to use its $.val() function as below:

    formData.append("myFile", $("#myFileField").val());
    

    But this is incorrect as it doesn’t return the whole File object, but merely the file name as String which is utterly useless as it doesn’t contain the file contents.

    If you don’t want to use document.getElementById() for some reason, then use one of the following instead:

    formData.append("myFile", $("#myFileField").prop("files")[0]);
    
    formData.append("myFile", $("#myFileField")[0].files[0]);
    

    An alternative is to use the jQuery Form plugin. Your entire form, when written and functioning properly without any line of JavaScript code, will then instantly be ajaxified with just the following line:

    $("#formId").ajaxForm(function(response) {
        // Handle Ajax response here.
    });
    

    It also supports file uploads as well by a hidden iframe trick. See also this jQuery Form documentation for an in-depth explanation. You may only need to change the servlet code to be able to intercept on both normal (synchronous) and Ajax (asynchronous) requests. See also this answer for a concrete example: Simple calculator with JSP/Servlet and Ajax

    Either way, the uploaded file should then be available in the doPost() method of a @MultipartConfig servlet as follows:

    Part myFile = request.getPart("myFile");
    

    Or if you’re still on Servlet 2.5 or older, use Apache Commons FileUpload the usual way. See also this answer for a concrete example: How can I upload files to a server using JSP/Servlet?

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

Sidebar

Related Questions

I need to send a CSV file in HTTP response. How can I set
Can sendfile() linux syscall be used to send part of file from one mmaped
I want to send a xml file to my wcf service , how can
I can send any windows application key strokes with PostMessage api. But I can't
If webserver can send gzip response, why can't browser sent gzip request?
I have a upload file form, when you hit Submit it send the file
org.apache.http can work with multipart? I send http request with header Range : bytes=0-500,9000-9999
I have jquery script, which should send response to my file. If everything good,
Im trying to post to a multipart html form using CURLs CURLOPT_POSTFIELDS, but I
In Javascript I can send XML string to JSP server (XmlAction.jsp): Javascript Code: var

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.