0

In my Asp.net MVC application, I'm using the Jquery data table to show data. My problem is here you can see I'm used link inside data table to open the file. When the file not available in Downloads folder 404 server error shows, Instead of that I need to direct another page. How to check file available or not inside this datatable.File type was .html

<script>

    $("#cribTable").DataTable({

        "ajax": {

            "url": "/CribManual/GetAllDownloadedCribs",
            "type": "GET",
            "processing": true,
            "datatype": "JSON",
            "serverSide": true
        },
        "columns": [
            {
                "data": "ID", "render": function (data) {

                    return `<button type="button"  data-toggle="confirmation" data-title="Are you sure?" href='@Url.Action("DeleteCrib", "CribManual")/${data
                        }'" class="btn btn-danger">Delete</button>`;
                },

                orderable: false
            },
            { "data": "CUS_NAME" },
            { "data": "CUS_NIC" },
            { "data": "STATUS" },
            { "data": "LOCATION" },
            {
                "data": "LOCATION", 'render': function (data, type, full, meta) {
                   return '<a href="/Downloads/' + data + '">Read File</a>';
                }
            }
        ]

    });

</script>
Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68
  • If you're getting the links from a script on the server, I would advise that the script return a NULL value or some other value so a link is not created. Or, if `data` does not contain the correct value, link to another value. – Twisty Jan 03 '19 at 07:31

1 Answers1

0

This solution is not advised, yet you can capture error codes when performing AJAX calls. Reference: Capture 404 status with jQuery AJAX

This might look something like this:

{
  "data": "LOCATION",
  "render": function (data, type, full, meta) {
    var dUrl = "/Downloads/" + data;
    var result = $("<a>").html("Read File");
    $.ajax({
      cache: false,
      url: dUrl,
      success: function(dat, stat, xhr){
        result.attr("href", dUrl);
      },
      error: function (xhr, stat, err){
        if(xhr.status==404) {
          result.attr("href", "#").addClass("no-file");
        }
      }
    });
    return result.prop("outerHTML");
  }
}

DoS WARNING This will generate an HTTP request for every file that is being rendered in the table. If that is 3 or 4, your web server will be able to handle it. But if it is 10, 50, 100, 500, 1000 etc etc etc, you're going to execute a Denial of Service attack against your web server in essence. Most web servers are configured to allow 10 concurrent simultaneous connections to the server. You can up that in the configuration, yet you may end up running out of resources.

The server may not be able to open and close enough sockets to handle all the requests and will start queuing them. This will result in the browser having to wait longer to execute the script and could result in timeouts. Depending on the list size, the browser may begin to consume more memory resources and could potentially lock or crash.

Again, I would strongly advise having the server side script do the heavy lifting. It should not return data if the item does not exist on the server or it should return a value that the Datatable can use without having to do a lot of potentially threatening work.

This is coming from a place of experience not theory.

Hope that helps.

Twisty
  • 30,304
  • 2
  • 26
  • 45