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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:47:51+00:00 2026-05-22T17:47:51+00:00

Full code at https://gist.github.com/992562 . I am using HTML File API and drag/drop to

  • 0

Full code at https://gist.github.com/992562.

I am using HTML File API and drag/drop to upload files via XHR post to a PHP script. Procedurally, everything seems to be working OK, however when I try to open the uploaded files, any non-text file is much larger than the source file, and won’t open. It’s clearly not the same data as was on the source disk. However, text files are exactly the same and open just fine.

Some examples on a 3-file drag/drop upload:
file 1: text/XML: on disk 13 KB, uploaded 13 KB, works perfectly
file 2: image/PNG: on disk 14 KB, uploaded 18 KB, won’t open
file 3: application/XLSX: on disk 12 KB, uploaded 14 KB, won’t open

It all boils down to this (after xhr headers are setup, file object is ready, etc):

  var reader = new FileReader();
  reader.onload = function(evt) {
    xhr.send(evt.target.result)
  }
  reader.readAsBinaryString(f);

returning large, bad data. Is there anything clearly wrong with it?

  • 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-22T17:47:52+00:00Added an answer on May 22, 2026 at 5:47 pm

    This is probably because you’re reading the file as a binary string and constructing the multipart/form-data request manually. For one, you don’t need to use FileReader.
    Since you just want to send the content, try using xhr.send(File) or xhr.send(FormData). The latter constructs and sends a multipart/form-data for you:

    function uploadFiles(url, files) {
      var formData = new FormData();
    
      for (var i = 0, file; file = files[i]; ++i) {
        formData.append(file.name, file);
      }
    
      var xhr = new XMLHttpRequest();
      xhr.open('POST', url, true);
      xhr.onload = function(e) { ... };
    
      xhr.send(formData);  // multipart/form-data
    }
    
    document.querySelector('input[type="file"]').onchange = function(e) {
      uploadFiles('/server', this.files);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My dream IDE does full code hints, explains and completes PHP, Javascript, HTML and
Whilst refactoring some old code I realised that a particular header file was full
Using the sample app for rpx_now gem ( http://github.com/grosser/rpx_now_example ) on localhost:3000, I have
I have a directory full of legacy code from a VB6 application. I have
im re-factoring php on zend code and all the code is full of $_GET[this]
Line of code is: BreakDown(Full As String, FName As String, PName As String, Ext
When reading source code, I always want to know the full path of the
I've tried two different methods of reusing code. I have a solution full of
For example, will the first piece of code perform a full search twice, or
For full control of your application, do you prefer a GridView or a HTML

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.