What is the best way to implement rate limiting for my AJAX upload requests and enable support for multiple files at the same time

I have recently developed a file upload script for my server.

My main objective is to enable support for multiple file uploads and chunking, especially since the files can vary in size from 5Mb to 2Gb.

The current obstacle I am facing involves all chunks of all files starting simultaneously once the upload process begins. This leads to browser lockup issues due to the limitation where Chrome allows only up to 6 requests per domain.

Below is the snippet of JavaScript code:

(function () {
  'use strict';
  var status = document.getElementById('status'),
  uploaders = [],
  upload,
  chooseFile;

  upload = function (blobOrFile, chunk_num, chunk_total, file_name) {
    var uXHR;

    uXHR = new XMLHttpRequest();
    uXHR.open('POST', '/upload/url/', true);
    uXHR.upload.onprogress = function (e) {
      if (e.lengthComputable) {
        console.log(Math.round((e.loaded / e.total) * 100));
      }
    };
    uXHR.onloadend = function (e) {
      uploaders.pop();
      if (!uploaders.length) {
        status.appendChild(document.createTextNode(' All Done! '));

        allFilesUploaded(file_name, chunk_total);
      }
    };
    uploaders.push(uXHR);
    uXHR.send(blobOrFile);
  };

  chooseFile = document.getElementById('file-input');
  chooseFile.addEventListener('change', function (e) {
    var self = e.currentTarget;
    var a;
    for ( a=0; a<self.files.length; a++ ) {
      var blob = self.files[a],
      bytes_per_chunk,
      SIZE,
      num_chunks,
      start,
      end,
      fileName;

      fileName = self.files[a].name;
      bytes_per_chunk = parseInt(67108864, 10); // 64mb
      SIZE = blob.size;
      num_chunks = Math.max(Math.ceil(SIZE / bytes_per_chunk), 1);
      console.log('Sending' + num_chunks );
      start = 0;
      end = bytes_per_chunk;
      var i = 1;
      while (start < SIZE) {
        upload(blob.slice(start, end), i, num_chunks, fileName);
        start = end;
        end = start + bytes_per_chunk;
        i++;
      }
    }
  }, false);
})();

Here is the basic HTML structure:

<input type="file" id="file-input" multiple="multiple" />
<p id="status"></p>

My goal is to establish a queue system for uploading chunks, with a maximum of 6 concurrent uploads running at any time.

In addition, I want to incorporate a callback function for when each file completes its upload, ideally with a global percentage completion feature. However, I am uncertain about how to implement this, especially since I have only been able to calculate progress for individual chunks.

Answer №1

If you're in search of a great tool for your specific needs, I recommend checking out dropzone js. It excels at fulfilling the exact task you have in mind and boasts a user-friendly interface. Additionally, it offers customizable settings for handling concurrent uploads and provides a robust API to manage any potential failures seamlessly.

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

Utilize Angular to organize data in a single-dimensional array

In my project, I am using Angular and Bootstrap to create a dynamic data grid where users can edit the data. The dataset consists of an array of objects, with each object containing a non-unique group property to categorize records. For example: [ { ...

What does the reportProgress function do in HTTP services with JavaScript?

Can someone explain the functionality of reportProgress in JavaScript, specifically when used with Angular Typescript? I am having trouble finding documentation on this. return this.httpClient.request<ProductResponse>('get',`${this.basePath ...

Using CodeIgniter to send input file information through AJAX

I am currently working on a form that includes fields for title, price, image, and category. The form successfully inserts the strings into the database and uploads the image without any issues. Now, I aim to implement this process using ajax. While the a ...

Step-by-step guide to linking/referencing multiple scripts in an HTML file using React (no NODE.js required)

I'm currently working with React.js without Node.js, and I'm facing an issue linking multiple script files in my HTML file. Here is a snippet of my code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> ...

What are the benefits of storing dist in both a GitHub repository and on npm?

I'm curious about why some repositories include a dist folder. Shouldn't repositories just store source code and not any builds or compiled files? Let's take a look at an example with ES6 code. package.json { "files": [ "dist", ...

The AngularJS change event is not being activated

I am a beginner with angular js and I have implemented a bootstrap calendar in my application. However, I am facing an issue where the change event is not being triggered when the month changes, no matter where I place it within the code. Here is the snip ...

Requesting Ajax data results in an undefined response on the second attempt

While working with MVC3 and C#, I encountered an issue where the parameters received by the C# action method are null on the second call, even though everything works fine initially. This is how my code looks: Monitor.SearchAction = function (pageNum) { ...

Removing a table row on a PHP page (without affecting the database) and ensuring that it does not reappear even after refreshing the page

On one of my pages, I have a script that fetches data from the database and displays it in a table: $statement = $connection->prepare($query); $statement->execute(); $result = $statement->fetchAll(); $data = array(); $filtered_rows = $statement-& ...

Page redirects automatically after AJAX call

I'm a beginner when it comes to using AJAX and I am trying to delete a student from a list through an AJAX request. I want the response of the request to be displayed on the same page within a specific div, but instead, the response keeps redirecting ...

Enhance the editing capabilities of the Json data form

https://i.stack.imgur.com/YZIjb.png My goal is to enhance a form for editing json data by moving beyond the typical <textarea /> tag and making it more user-friendly. Are there any tools available that can help improve the form's usability? Add ...

Is it possible to bind HTML within an AngularJS partial but encountering an inexplicable issue?

I'm facing an issue with my ng-repeat that involves using a partial HTML file to loop through and display items on a page. The variables within the partial are not displaying, but interestingly enough, the variable {{rule.name}} outside of the partial ...

Navigating Gmail pop-up windows can be tricky. Take a look at the

Looking for tips on dealing with Gmail popups? Check out the screenshot below. https://i.sstatic.net/5m1OK.png ...

NPM Package Encountering Module Parsing Issue

I encountered a strange error while building my project with Webpack. The error is related to the Got package that I am trying to import from its `package.json` file. Module parse failed: .../node_modules/got/package.json Unexpected token (2:8) You may ne ...

Obtaining the final href/link without the need to click on it in a javascript call using Selenium

Currently web scraping a lengthy table of HTML links (permitted under Terms of Service). However, all the links are JavaScript calls (href="javascript:;"), so using get_attribute() to retrieve the link will not be effective. I am trying to avoid actually c ...

I need help determining the starting date and ending date of the week based on a given date

I am looking to determine the starting date (Monday) and ending date of a specified date using Javascript. For instance, if my date is 2015-11-20, then the starting date would be 2015-11-16 and the ending date would be 2015-11-21. ...

Using Sweetalert2 to send data via AJAX POST request

Recently, I've been incorporating SweetAlert2 into my project and I want to create an "Add Note" feature. The process involves the user clicking a button, being directed to a page, and then the following script is executed: <script>swal({ ...

Retrieving HTML values from the database and displaying them on the website after truncation

Storing HTML values in my database and then displaying them on my site has brought up an issue. Sometimes the HTML value obtained contains a "cut off" HTML, which I have no control over since it is retrieved from another website. An example of this cut-off ...

An issue occurred with the session being undefined in Node.js Express4

I'm encountering an issue where my session is undefined in new layers even after setting the value within an "if" statement. /*******************************************/ /**/ var express = require('express'), /**/ cookieParse ...

JavaScript API Response - conditional statement for handling a 'null' response

Does anyone have any suggestions for the following scenario: I have a response in .json format containing personal data of a person, who may or may not be assigned to a project. Here is an example response where the person is not assigned to a project: & ...

Executing npm run build index.html results in a blank page being generated without any error messages or warnings

After building my react app with npm run build, I encountered a problem where clicking on index.html resulted in a blank page opening in the web browser. I explored several solutions to address this issue but none seemed to work. Some of the strategies I ...