Unexpected failure when using FormData in Ajax callback for large files

I have a file upload control where I can upload various types of files. After getting the file, I store it in a FormData object and make an ajax call to my controller. Everything works well with images and small .mp3 files. However, when I try to upload .mp3 files larger than 5MB, it triggers the error function.

Here is a snippet of my code :

 document.getElementById('fileUploadControl').onchange = function () {

        var data = new FormData();
        var files = $("#fileUploadControl").get(0).files;

        for (var i = 0; i < files.length; i++) {
            data.append("UploadedFile" + i, files[i]);
        }

        var ajaxRequest = $.ajax({
            url: '/Main/HandleFileUpload/',
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            type: "POST",
            success: HandleSuccess,
            error: HandleError
        });

        ajaxRequest.done(function (xhr, textStatus) {
        });
    };

 <input type="file" multiple id="fileUploadControl" style="width:0px;height:0px" />

Any suggestions on how to solve this issue?

Answer №1

Finally, I figured it out with a little help from @techexpert

I made adjustments to the uploadFileSizeLimit in the application settings

<appSettings>
   <add key="uploadFileSizeLimit" value="1048576" />
</appSettings>

Answer №2

The reason for this restriction is primarily due to the set server-side post size limit, put in place for security reasons. When utilizing Tomcat, users have the ability to adjust the connector settings in server.xml to change the maxPostSize value - typically set at 2MB by default.

This limitation pertains to servers running ASP, JSP, or PHP, although the specific configuration file may differ depending on the server being used.

Answer №3

When the 'fileUploadControl' element is changed, the following function is executed:

    var data = new FormData();
    var files = $("#fileUploadControl").get(0).files;

    for (var i = 0; i < files.length; i++) {
        data.append("UploadedImage" + i, files[i]);
    }

    var ajaxRequest = $.ajax({
        url: '/Main/BroadcastFileUpload/',
        data: data,
        cache: false,
        contentType: false,
        processData: false,
        type: "POST",
        success: LoadImages,
        error: OnLogoFail
    });

    ajaxRequest.done(function (xhr, textStatus) {
    });
};

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

Subclass declaration with an assignment - React.Component

I recently started going through a React tutorial on Egghead and came across an interesting class declaration in one of the lessons: class StopWatch extends React.Component { state = {lapse: 0, running: false} render() { const ...

Is it possible to use HTML elements to trigger actions in order to display or conceal divs using JavaScript?

Beginner - I am currently working on a webpage using TinyMCE wysiwyg and I would like to implement a functionality where I can display or hide certain divs upon clicking a link/button. Unfortunately, due to the structure of the page, it seems that I ca ...

Warning: Shadcn-UI Form Alert - An element is converting an uncontrolled input to controlled mode

Throughout the course of this project, I found myself repeatedly using const [fileNames, setFileNames] = useState<string[]>([]); and logging the state with console.log(fileNames). However, every time I clicked on the parent element, an empty Array e ...

Utilizing Django to determine the appropriate view based on whether it is a GET request or an AJAX POST method

In my Django project, I have a view that serves different purposes depending on whether the request method is GET or POST. When it's a GET request, the view simply renders the page for the user. On the other hand, when it's a POST request, I use ...

Revamp the jquery.ajax promise or find an alternative solution

I am looking to modify the data from an ajax call and pass it to another plugin's constructor. I believe that by having the plugin options accept either an object for its data or a function that returns the data, I can incorporate a deferred object li ...

Enhancing fancybox with dynamic scrollbars as the window size changes

I'm currently working on a popup form that I'm displaying with Fancybox 2 by using the code below: $(link).fancybox({ type: 'iFrame', autoSize: false, autoScale: false, width: '1280px', height: '1024p ...

Having trouble getting CSS styles to work properly in conjunction with Javascript code

Currently, I am developing a feature on a canvas that covers the entire webpage's width and length. In this canvas, I have the ability to create boxes by clicking down anywhere on the canvas, dragging my mouse in any direction, and upon releasing the ...

Building a React component that's a table containing rows that can be scrolled and elements that are clickable

Imagine having a set of events and their respective participants: const myData = { "event_1": ["1", "2","3"], "event_2": ["11", "5","8", "9"], "event_3&quo ...

Creating dynamic elements in JavaScript and assigning them unique IDs

Hi there, I'm currently working on a project that requires generating dynamic divs with a textbox and delete button inside each one. The challenge I'm facing is figuring out how to assign a unique ID to each textbox element so that I can properly ...

Tips on personalizing the default html template in nuxt

Struggling to customize the default page from my Nuxt app due to limited documentation. Link to Documentation I'm interested in extracting only certain elements like the title or links in the head section. For example, how can I access just the li ...

how to name a collection variable in MongoDB shell using JavaScript

When using the mongo shell with JavaScript, is it possible to dynamically set the collection name in order to work with multiple collections? db.collection.insert() ...

Encountering excessive re-renders while using MUI and styled components

Hey there! I recently worked on a project where I used MUI (Material-UI) and styled-components to render a webpage. To ensure responsiveness, I made use of the useTheme and useMediaQuery hooks in my web application. My goal was to adjust the font size for ...

Creating an interactive star rating system with JSON data

Currently, I am in the process of developing a star rating system specifically designed for restaurant reviews. The 'Attributes' displayed on the user interface are dynamic and fetched in JSON format. There exist five attributes with each having ...

Expand the table in the initial state by using CSS to collapse the tree

I am struggling to collapse the tree view and need assistance. Below is the code snippet I've added. My goal is to have each node in the tree view initially collapsed and then expand a particular node on user interaction. For example, when I execute ...

Employ the Google Charting library for creating a GeoChart that is currently not displaying

Hello everyone. I'm having a bit of an issue with my web page development. I've been trying to add a GeoChart, but no matter how many times I copy-paste the code from the Google developer's website, the map just won't show up. I must be ...

Angular failing to append hash to ng-href in browsers that do not support it

When I attach an ng-href to a link like this: ng-href="{{post.btn.url}}" The resulting value is: ng-href="/news/some-post" In browsers that do not support html5 mode, these links do not work properly because they require a #. How can I handle this in I ...

Exiting callback function in JavaScript

Is there a way to retrieve the return value from within a node.js/javascript callback function? function get_logs(){ User_Log.findOne({userId:req.user._id}, function(err, userlogs){ if(err) throw err; if(userlogs){ ...

Using jQuery, you can easily apply a new class to a div when it is

I'm currently facing an issue with adding a class of "active" to a div upon clicking it using jQuery. My goal is to apply the css class of active in order to give the button a distinct color (or the hover effect.) Unfortunately, I have been unsuccess ...

A ServiceWorker used a promise in FetchEvent.respondWith() that resulted in a non-Response value of 'undefined'. This caused an issue in browser-sync

There are times when my server encounters an error in the console: Failed to load ‘http://localhost:3000/browser-sync/socket.io/?EIO=3&transport=polling&t=Lm2wn4p’. A ServiceWorker passed a promise to FetchEvent.respondWith() that reso ...

Is there a different option for managing roles in ASP.NET MVC than Role Provider?

I am seeking an alternative to the Role Provider and Membership Provider as I find them to be cumbersome. Instead, I am working on creating a more streamlined and flexible version of my own. My question is, are there any suitable alternatives to the Role P ...