The issue arises with XMLHttpRequest when there are discrepancies between the event.loaded and event.total values

When using XMLHttpRequest for file upload in the browser, a progress bar is implemented to show the amount of the image that has been uploaded. The following code snippet demonstrates how this is achieved:

xhr.upload.addEventListener('progress', onprogressHandler, false);

function onprogressHandler(event) {    
  resp.innerHTML = event.loaded +' and '+ event.total;

  var percent = Math.round((event.loaded / event.total) * 100);
  var calc_display = document.getElementById('calc');
  calc_display.innerHTML = percent;
};

Despite setting up the progress bar, the issue arises when the values of event.loaded and event.total remain static, typically around 2,700,000 kB. This raises concerns among newcomers as to why these values do not change during the upload process.

As a beginner in utilizing XMLHttpRequest for file uploading, it is important to troubleshoot and identify potential problems that may cause this inconsistency in values. Further investigation is required to determine the root cause of this discrepancy and enable a successful file upload process.

Answer №1

In the documentation for the progress event, it is noted that a progress event can include a boolean attribute called .lengthComputable. If this attribute is false in a progress event, it indicates that the values of event.loaded and event.total may not accurately represent the upload progress.

According to the specification, if the Content-Length header of the XMLHttpRequest is set, then .lengthComputable will be true and total will reflect the file length. However, the XMLHttpRequest does not allow setting the Content-Length header - details on this can be found by searching for "Content-Length" in the XMLHttpRequest documentation.

If you examine the requests received by your server, check for the presence of the Content-Length header.

Answer №2

If the size of your uploaded video or image is small, the values may end up being the same. Test this by uploading a file larger than 70MB and comparing the loaded and total values - they should differ, indicating that it's working correctly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing the React state not to update properly?

Having an issue with my Alice-Carousel in react. I'm fetching items from an API and updating the array for the carousel, but the value of items keeps coming back as undefined. Using CryptoState context to avoid prop drilling. import React from 'r ...

Steps to load data using ajax-php with specific parameters

I have a situation where I need to trigger a JavaScript function when a link is clicked. <input type='hidden' id='name'> <a href='#' onclick='getUsers(1)'>Click here</a> function getUsers(id){ $( ...

Making an asynchronous request from jQuery to a Slim Framework API endpoint

I'm currently working on incorporating Slim into my project and I'm facing some challenges with setting up the AJAX call correctly: $.ajax({ url: "/api/addresses/", type: 'POST', contentType: 'application/j ...

Ways to access a function variable within an AJAX `done` function

This is the JavaScript function I am working with: $('.editable').change(function () { event.preventDefault(); var el_text = this.lastElementChild; var action = this.action; var method = this.method; var data = $(this).serialize(); ...

Attempting to pass a limit argument to a query in urql, Strapi, and Next JS for the Query.posts field has resulted in a combination of errors including graphQLErrors

I need help figuring out how to pass a variable for the limit parameter in my GraphQL query. I am currently working with urql, Strapi, and its GraphQL plugin in a Next.js application. My goal is to introduce a variable for the limit to restrict the number ...

WordPress Error: Ajax Search Functionality

I am currently using WordPress for my website and have implemented Woocommerce for selling products. However, I encountered an error message as shown below when searching on the site: Catchable fatal error: Object of class WP_Error could not be converte ...

Dealing with all errors across all pages in Angular using a comprehensive error handling approach

I am currently using AngularJS and attempting to capture all errors across all pages. However, I am encountering an issue where it does not catch errors in some instances. For example, when I trigger a ReferenceError in one of the controllers, it is not be ...

How to emphasize a portion of an expression in AngularJS using bold formatting

I have a specific goal in mind: to emphasize part of an angular expression by making it bold. To achieve this, I am working with an object obj which needs to be converted into a string str. obj = $scope.gridOptions1.api.getFilterModel(); for (var pr ...

Using JS to switch between text and state in ToneJS

I am facing an issue with the text not displaying in the "play-stop-toggle" element, despite my limited experience with JS. My desired outcome includes: [SOLVED] The text in <div id="play-stop-toggle" onclick="togglePlay()">Play ...

What methods can I use to retrieve traversed objects using riak-js?

Utilizing Node with riak-js to interact with a Riak database. I have established two buckets named invites and events. The invites bucket contains a link to events. Is there a way to fetch both the invite object and the corresponding event object in one qu ...

Stop the context menu from popping up when the element is right-clicked

Is there a way to create a custom right-click interaction for an element on my website, <div class="myElement"></div>? I want to avoid the default context menu from popping up when the user right-clicks on this element in order to enhance the u ...

How can I switch the visibility of two A HREF elements by clicking on one of them?

Let me break it down for you in the simplest way possible. First off, there's this <a href="#" id="PAUSE" class="tubular-pause">Pause</a> and then we have a second one <a href="#" id="PLAY" class="tubular-play">Play</a> Al ...

Tips for sending an array of Objects in a format that can be easily understood by jQuery Ajax

I am working with an asmx web service that includes a function called GetTags: [WebMethod] public List<Tag> GetTags() { List<Tag> TagList = new List<Tag>(); DataTable dt = Helpers.Tags.GetTags(); for ...

JavaScript Await Dynamic User Input

I have implemented a modified version of a script for generating random numbers in multiple columns of a spreadsheet, inspired by Tim Cooper's solution on stackoverflow. This script works by selecting a range of cells and running it from an onOpen men ...

Using a combination of nested fetch() and array.map() does not allow for the return

My previous coding experience didn't present any issues with rendering HTML. Typically, I only needed to use one fetch() function. However, in this particular scenario, it appears that I require two fetch calls. The first fetch retrieves a product ID ...

What is causing the router.events to not fire for FooComponent in my Angular project?

Upon opening the following link , the eventsFromFoo entries in the console are nowhere to be found. It appears that this.router.events is failing to trigger for FooComponent. Any insights on why this might be happening? I am in urgent need of capturing t ...

Can Sync blocking IO be implemented solely in JavaScript (Node.js) without the use of C code?

Is JavaScript intentionally designed to discourage or disallow synchronous blocking I/O? What is the reason for the absence of a sleep API in JavaScript? Does it relate to the previous point? Can browsers support more than one thread executing JavaScript? ...

Upon successful registration, users will be automatically redirected to their profile page

Having trouble with the redirection to the login page from my profile page, which is an HTML file and the main page is the login page. I've tried redirecting to both pages, but it always lands in the catch block whenever a redirect is attempted. angu ...

What is the best way to ensure jQuery loads before CSS code on a website

I am currently working on a blog project that allows visitors to customize the background color by simply clicking on one of five available buttons. $(function(){ //Checking if a color scheme has already been selected var chosenColor = ...

Are you struggling with the issue of Function components not being able to be given refs? When you try to access the ref, it just fails. Perhaps you should consider using React.forwardRef

I have implemented the "styled" feature from Material UI to add styles to my components. Right now, I am in the process of cleaning up code to remove console errors. Initially, I encountered this warning message: "Warning: React does not recognize the con ...