JavaScript FileReader encounters 'Request Entity Too Large' issue during upload

Dealing with files being removed from requests during ajax upload in Chrome has been a challenge for me.

When files are selected or dragged and dropped, I handle them by using FileReader:

    // initialise fileListData array
    fileListData = new Array(fileList.length);

    // read files
    for (var i=0; i<fileList.length; i++) {

        // update fileListData file status
        fileListData[i] = { 'name': fileList[i].name, 'processing' : 1 };

        // read file
        var fileReader = new FileReader();

        fileReader.onload = (function(idx) {
            return function(e) {
                // update fileListData data
                setFilesAreReady(idx, e.target.result);
            }
        })(i);

        fileReader.readAsDataURL(fileList[i]);
    }

The setFilesAreReady method simply stores the FileReader result in the fileListData array and then checks if all items have finished processing to enable the upload button.

Once file reading is complete, the upload button becomes enabled and clicking it triggers ajax requests:

if (is_chrome || is_safari) {
    formData.append('upfile_0', ajax_uploads[file_idx].fileData);
    console.log(ajax_uploads[file_idx].fileData);
} else {
    formData.append('upfile_0', file);
}
formData.append('event', eventVal);
formData.append('filePrivacy', filePrivacyVal);

First, I build a formData object and then submit it with the request:

    // begin upload
    jQuery.ajax(
    {
        data:       formData,
        url:        path_to_upload_script + '?upload_id=' + upload_id,
        type:       "POST",
        processData:    false,
        contentType:    false,
        error:
            function (jqXHR, textStatus, errorThrown)
            {
                ...
            },
        success: 
            function (response) 
            {
                ...
            }
    });

    // initiate ajax progress
    initialiseAjaxProgressBar(upload_id);
}

The issue arises when uploading large files, causing a Request Entity Too Large error:

https://i.sstatic.net/B9NX3.png

Despite the server's ability to accept files up to 250MB, this error occurs. Uber uploader (Perl script) manages large file uploads but the problem only affects Chrome and Safari, not Firefox or Internet Explorer.

I suspect the error may be due to an incorrect content type since I am uploading a data string instead of a JavaScript File object.

How can I effectively upload the FileReader result (e.g., data:video/mp4;base64,...) along with other parameters attached to formData in the ajax request?

Answer №1

After much research, I ultimately decided to utilize the innovative plupload library for my file uploading needs. This ingenious solution breaks down files into 1mb segments in order to circumvent the issue of large request payloads. As a result, the server is able to reconstruct the complete file seamlessly. Moreover, this approach facilitates the upload of exceptionally large files without exceeding PHP's maximum post size constraints with each individual chunk.

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

Using Angular 5 to access a variable within a component while iterating through a loop

I am currently in the process of transferring code from an AngularJS component to an Angular 5 component. Within my code, I have stored an array of objects in a variable called productlist. In my previous controller, I initialized another empty array nam ...

How exactly does this JavaScript code determine the index of the maximum value within an array?

Examining the specific line of code: let largest = arr.reduce((a,v,i) => v > a[1] ? [i,v] : a, [-1,0]); I find this part a, [-1,0] particularly perplexing, as I am unsure of its syntax. How does a function before the comma? ...

Ending a timed function in AngularJS 1

As part of my Angular JS 1 learning journey, I am working on a small test involving text areas that display text using Angular functions when a user enters and exits them. The enter function has a 3-second delay, while the exit function waits for 5 seconds ...

Display Tooltip for Angular (ng2-tooltip-directive) exclusively when the value is defined

I have integrated the Tooltip for Angular (ng2-tooltip-directive) into my application to display tooltips. Given the code below, how can I ensure that tooltips are hidden whenever the title is undefined? //HTML <div tooltip="{{title}}"></div> ...

Error in jQuery: Unexpected token ILLEGAL during Asp.Net page loading

I am encountering an issue with using jQuery where even simple functions are causing an error message Uncaught SyntaxError: Unexpected token ILLEGAL to appear on page load. Unfortunately, I do not possess enough knowledge about jQuery to troubleshoot such ...

What is the purpose of using $ symbols within NodeJS?

Lately, I've been attempting to grasp the ins and outs of using/installing NodeJS. Unfortunately, I'm feeling a bit lost due to tutorials like the one found here and their utilization of the mysterious $ symbol. Take for instance where it suggest ...

Using AngularJS to implement a clickable functionality within a directive

Currently, I am in the process of implementing a drag-and-drop directive. When an element is dropped, I am adding a copy of that element to my div and appending the ng-click attribute to it in this manner: copy.append('<button class="close" ng-cli ...

implementing multiple fields in jquery ui autocomplete

I have encountered an issue with the code where the second input field is not displaying images along with text suggestions. Can someone please review the JavaScript and suggest any necessary changes to make it work correctly? Example queries: clinton, bu ...

After the initial iteration, the .length function ceases to function properly when

A validation method is set up to check the length of a user input field. It functions properly on the initial try, but does not update if I go back and modify the field. $("#user").blur(function () { if ($("#user").val().length < 3) { $("#userval ...

"Mongodb and mongomapper are powerful database tools often used

I am currently working on a Rails app that uses ActiveRecord to manage products. Each product has a category and subcategory, with each subcategory defined by multiple fields within the application. This system has become quite complex and I have been cons ...

What are the steps for implementing webpack 5 configurations in Next.js?

I can't seem to figure out how to properly add experiments to my webpack config. Here is my current environment: [email protected] [email protected] To set up, I started a new Next.js app using the command npx create-next-app blog Accord ...

Use jQuery to retrieve the response from a JSON request and showcase it on the existing HTML page

Currently, I am working on a project that involves integrating a JSON-based web service from a remote server. The method of this service can be accessed by using specially formatted URLs such as: http://root-url/ws/service-name?request={json-string} The ...

Discover the best method for retrieving or accessing data from an array using Angular

In my data processing task, I have two sets of information. The first set serves as the header data, providing the names of the columns to be displayed. The second set is the actual data source itself. My challenge lies in selecting only the data from th ...

Uncovering the Magic of TypeScript Type Inference with Generics and Function Objects

As I attempted to create a versatile function that can accept an interface containing both functions and data, there seems to be an issue with inference. Assistance in resolving this problem would be greatly appreciated. To view the code that is failing t ...

Activate dynamic form fields using Ajax

Consider a scenario where the code looks like this: <div id="stuff<? echo $dynID; ?>" class="bla"> <form id="myform<? echo $dynID; ?> " action="bla.php"> <input name="iname<? echo $dynID; ?>" value="<? echo $row[1] ...

Tips for handling TypeError when testing formgroups in Angular unit tests---How to address TypeError

While attempting to conduct unit testing on my form fields in Angular, I encountered an issue with Karma stating that my property is undefined. When I logged the formGroup, it returned as undefined. However, upon logging my component, all parameters were r ...

What is the best way to format the information when using response.send() in express.js?

I need help with customizing the content I'm returning in the app.post() method. Is there a way to do this? Below is an example of the app.post() code: app.post("/",function(req,res){ const query = req.body.cityName; const cityName = query.charA ...

Switching from dark mode to light mode when reloading or navigating to a new page

Hello everyone. I've successfully implemented a dark mode toggle on my website, but I'm facing an issue where the mode resets whenever I navigate to a new page or refresh the current page. Can anyone help me figure out how to prevent this from ...

Jquery for controlling the navigation menu

I'm new to JavaScript and facing 3 challenges: 1. When I click on the parent <li>, the correct content of the child <sub> does not show up. Each category like "colors, shapes, sizes" should have corresponding child categories like "green, ...

Tips on how to reach an Angular component within a third-party library

I'm currently working on a web application that incorporates the Deezer player through the official JS SDK. I've run into an issue where I am unable to access my Angular component from within the Deezer event subscription. The arrow function does ...