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

Tips for showing data from Ajax responses on an HTML page

In my code, there are two JavaScript functions: The function fetch_items is responsible for returning data in the form of stringvalue. On the other hand, the function extract_items($arr) passes values to the fetch_items function. function fetch_items ...

Performing calculations with jQuery to extract the value of a selected dropdown option and a text

I am attempting to calculate the total amount by multiplying the value entered in a text box labeled qty1 by the price retrieved from a select box using ajax. <script type="text/javascript" charset="utf-8"> $(document).ready(function(){ ...

The functionality of the Bootstrap carousel for moving to the next and previous images is malfunctioning, as it only

The carousel on my website is not functioning properly. It only displays the first image and does not slide to the next pictures as it should. Here is the code snippet for the carousel: <body> </nav> <div id="carousel1" class="carousel slid ...

Why does my Visual Studio Code always display "building" when I launch an extension?

https://code.visualstudio.com/api/get-started/your-first-extension I followed a tutorial to create a hello world extension. Why does my VSCode always display 'building' when I run the extension? Executing task: npm run watch < [email p ...

Retrieving information from a webpage and implementing it in an Android Java application

I'm currently working on an Android app project and have extensively used Jsoup in the past. However, this time I am facing a challenge as the results seem to be empty. It seems that this is due to the web page utilizing AJAX. How can I retrieve data ...

What is the best way to ensure that messages stay saved in my app for an extended

I am looking for a way to store messages sent through my small messaging app in a persistent manner. Currently, the messages are transferred from the front-end to a Node, Socket.io, and Express back-end. A friend recommended using Enmaps (), but unfortuna ...

"Troubleshooting JSON parsing issues with simple_form and AJAX integration in a Ruby on

Currently, there is a form on the users dashboard that allows them to add a custom game rule. This form utilizes Simple Forms and remote => true, loaded via Jquery's .load() function from the "rules" view, which is managed by the rules controller. Up ...

Incorporate action icons (such as edit and delete) into a table in React.js using material-ui

Within my existing table, I utilized a library known as react-bootstrap-table-next This library effectively displays data in a table format with values originating from a JSON response Now, my goal is to integrate an Action column that includes options f ...

Integrating PHP code into a React.js application can provide

I am currently integrating react.js into a section of my app and exploring the possibility of embedding some PHP code into react.js. This would allow me to avoid having to completely rewrite the backend that was originally written in PHP. Here's an ex ...

Can Cell be rendered into a targeted element?

Can a Cell from CellJS be rendered into a specific HTML element? For example, including a Cell alongside some static HTML that is not managed by cell. Or having two separate Cell apps on a single page. <!DOCTYPE html> <html> <header> ...

Difficulty with Horizontal Mousewheel Scrolling

Struggling to implement a horizontal scrolling feature (via mousewheel) for both containers in the code below. I want this feature to be easily applied to any future container creations as well. <body> <style> #container { display: flex ...

Communication between portlets cannot be cast to an ActionResponse

I am currently in the process of developing a web application on Liferay 6.2 using JSF Primefaces 5.3, running on Tomcat. The challenge I am facing involves having Portlet 1 and Portlet 2 situated on the same page. My objective is to manipulate the state ...

Enhancing File Upload with JSF 2.2 using h:inputFile and f:ajax

When incorporating h:inputFile with f:ajax and attempting to deploy it on Apache-tomcat-7.0.8, an exception is raised by the tomcat server when an AJAX request is made. Assistance in resolving this issue would be greatly appreciated. Thank you. javax.ser ...

The Lifecycle of an ASPX Page when Invoking [WebMethod]s

Currently, I am utilizing jQuery ajax to call multiple methods that have been decorated with [WebMethod]. These methods rely on a database connection established in an external library, which remains consistent across all functions. In the initial impleme ...

Exploring the possibilities of JavaScript within the IntelliJ REST client

I've delved into the extensive documentation provided by JetBrains on their HTTP Client and how to incorporate requests using files with a .http extension. The challenge I'm facing involves utilizing a function from a separate .js file within on ...

What is the best way to pause function execution until a user action is completed within a separate Modal?

I'm currently working on a drink tracking application. Users have the ability to add drinks, but there is also a drink limit feature in place to alert them when they reach their set limit. A modal will pop up with options to cancel or continue adding ...

Creating a new session in Node.js for every request

Our team is currently developing a nodejs application that requires the maintenance of sessions. We have implemented express to handle node variables, but we are facing an issue where a new session is created every time we hit a new service. The structure ...

How can the combination of Django and AJAX be utilized most effectively?

Currently, I am tackling my first major project and grappling with the integration of Django with ajax. My website features multiple standalone services written in JavaScript, however there are instances where information needs to be sent to the server. Ad ...

Why is the lower sub-component positioned above the rest of the page?

I have set up a sandbox environment to demonstrate an issue that I am facing. The main problem is that when I click on "Option 1" in the main menu, a new component appears where a bottom sub-component (named BottomControls.js) is displayed at the top of t ...

Locating the JSON path within an object

Need help with checking if a specific path is present in a JSON object? var data = { "schemaOne": { "name": "abc", "Path": "i.abc", "count": 5347, "subFolders": [ ] }, "schemaTwo": { "name": "cde", "Path": "i.cde", " ...