Ajax operates by fetching data from a database without requiring large files to be retrieved

Every time I try to retrieve a file larger than 0.5MB from the SQL database using AJAX, an error occurs. What am I doing wrong here?

$(document).ready(function () {

loadFileData();


  });

  function loadFileData() {

$.ajax({
    type: "GET",
    url: "/File/FileIndex",
    dataType: "JSON",
    success: function (data) {
        $.each(data, function (i, val) {
            var trow = $('<tr/>').data("id", val.id);
            var trowa = $('<tr/>');
            var trowb = $('<tr/>').data("id", val.id);
            trow.append('<td colspan="2"><a href="#" class="FileDownload">' + escape(val.Name)  +'</a></td>');
            trowa.append('<td><input type="file" id="FileUpload" /></form></td>');
            trowb.append('<td><input type="button" class="btnUpload" value="Upload File" /><input type="button" id="btnClear" value="Clear" /></td>');

            tab.append(trow);
            tab.append(trowa);
            tab.append(trowb);

        });
        $("#showFiles").html(tab);
    },
    error: function (error) {
        alert("Failed! Please try again." + error);
    }
});
var tab = $('<table style="width:100px" border=1 class=MyTable></table>');
   }

This is my controller:

//GET: File
    public JsonResult FileIndex()
    {

        List<tblFile> fileList = new List<tblFile>();
        using (FileDBEntities db = new FileDBEntities())
        {
            fileList = db.tblFiles.ToList();
        }

        return new JsonResult { Data = fileList, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    }

It works perfectly with small files, but why does it fail when trying to retrieve a file larger than 0.5MB? The error message I am receiving is:

function(){if(a){var t=a.length;(function r(t){v.each(t,function(t,n){var i=v.type(n);i==="function"?
(!e.unique||!c.has(n))&&a.push(n):n&&n.length&&i!=="string"&&r(n)})})(arguments),i?o=a.length:n&&(s=t,l(n))}return this}

Answer №1

My suggestion is:

In your Controller, consider returning the data as binary or a file Stream.

//GET: File
public JsonResult FileIndex()
{

    List<byte> fileList = new List<byte>();
    using (FileDBEntities db = new FileDBEntities())
    {
        fileList = db.tblFiles.ToList();
        
        foreach(var itemFile in fileList) {
           fileList.add(File.ReadAllBytes(itemFile));
        }
    }
    return new JsonResult { Data = fileList, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}

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

Interactive World Map with Fluid Motion Animation built using HTML, CSS, and JavaScript

I am in need of an uncomplicated way to visually represent events taking place around the globe. This involves creating a 2D image of a world map, along with a method to show visual alerts when events occur at specific geographical coordinates [lat, lng]. ...

The server failed to respond to the Angular HTTP request

To store data in my database, I have created a function in my component.ts file that invokes a service: ajoutText(newtext: String) { this.dataService.sendtext(newtext); } On the HTML side, it looks like this: <form class="form"> <mat-form-f ...

Call order for importing and exporting in NodeJS

This question is related to "code theory." Let's explore a scenario where I am utilizing the global namespace in a package. The structure includes a main entrypoint file, classes that are exported, and utility files used by the classes. Here's a ...

Having trouble receiving a POST response with Retrofit2

I recently started developing for android and I'm encountering an issue where my server response always goes to the onFailure block. I followed a tutorial but modified my code based on my preferences. The goal is to send a username and password to my ...

Does the CSV stream parser (PapaParse) cause rendering delays?

Currently, I am utilizing papa parse to fetch csv streams from my backend in order to visualize data. However, I have observed that while it is successfully invoking the callback for the data chunks, it is also causing rendering issues. I am attempting to ...

Get documents from Google Drive onto Xamarin Android Player

While developing a Cocossharp application in Xamarin, I ran tests on the Xamarin Android Player. Although it was functional, I encountered an issue with uploading json files from the Emulator File System to my application. I searched for a solution to tr ...

Sending images seamlessly between a C++ and C# application using sockets

I am facing a challenge with a server application written in C++. The application takes screenshots that need to be transferred to a client written in C#. However, there are a few issues that I need to address: Transferring the BMP image directly is too ...

Sending massive file using axios in React and .NET Core

I am facing an issue when trying to upload a large file (100MB) on the iis server. The code below works perfectly fine with smaller files (13MB), but I encounter the following error message when attempting to upload larger files: Access to XMLHttpRequest ...

Postback in C# webpage

Can we prevent the page from refreshing when a click event occurs, depending on certain validation criteria? ...

What causes the c# wrapper of c++ to crash the web app but function properly as a console app?

I am experimenting with integrating the C# wrapper of the C++ Spotify library into a web application. https://github.com/jonasl/libspotify-sharp Interestingly, my code compiles and runs fine as a standalone console app. However, when I attempt to execute ...

"Instead of sending JSON to the AJAX jQuery request, the PHP `json_encode` function is used

Creating a simple contact form as a WordPress plugin, I have developed a form as a popup within the same page: <form method="post" id="w_form" enctype="multipart/form-data"> <label for="first_name" class="text-secondary">First Name</la ...

Personalized dropdown filters for Bootstrap Datatable

In my codeigniter project, I am utilizing Bootstrap datatables. In the footer section, I have included the datatables js and initialized it like so: $('.datatable').dataTable({ "sDom": "<'row-fluid'<'span6'l ...

having trouble retrieving information from the input field

I'm having trouble retrieving the user's input from the input field, can someone assist me with this issue? I can't seem to identify what the problem is here. var ftemp = document.getElementById("Farenheit").value; <td> <i ...

Modify the message in the current alert box once you click on the OK button

After pressing OK on the existing alert, I aim to update the text without the modal closing upon clicking any button. Is it possible to achieve this without needing to display a new alert? Can we leverage PreConfirm to achieve this? Grateful for any assi ...

An error message is displayed when attempting to retrieve a json response

After implementing my flask app, I noticed that the following code snippet is being returned: return json.dumps({'status': 'OK','url': 'www.blahg.com'}) Upon inspecting my javascript code, I found it to be structur ...

Choosing specific content from a different div in JavaScript based on the line number

I have a question regarding two div elements, div1 and div2. I am trying to develop a function where in div2, I can specify the line number of div1 and display the relevant content from that specific line. Currently, I have created a function that successf ...

Tips for sending multiple variables using XMLHttpRequest

I want to pass two variables through an Ajax request to my ajax.php: var xhrRequest = new XMLHttpRequest(); xhrRequest.open ( "GET", "ajax.php", false ); xhrRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); x ...

Should we be validating passwords through mongoose middlewares - is this the right approach?

I am currently using the validator package for email validation in my project. const validator = require('validator'); email: { type: String, required: [true, 'User must have a email'], unique: true, lowercase: true, / ...

Conceal the vacant area located at the top of the page

By clicking on the "Run code Snippet" button or visiting this link, you may notice a significant amount of empty space at the beginning of the page. The images only become visible once you start scrolling down. I am looking for a solution to hide this emp ...

Implementing a personalized filter onto the footer of an AngularJS UI Grid

After successfully creating a custom filter for my ui-grid that limits decimal numbers to two places and exporting it as a pdf using ui-grid-exporter, I encountered an issue. The filter works fine when exporting the main ui-grid but fails to apply within t ...