Force file download

I am using jquery datatables to tabulate data from mysql. What is happening, is that when a user clicks on the download file link, the file is opening in the browser instead of downloading. I have included my code and would be grateful if someone could point out my error. Many thanks

$(function() {
  //$.fn.dataTable.moment('d/m/y H:i:s');
  var link = '<i class="fa fa-download fa-lg" style="align: middle; font-size: 1.8em; color: black; margin-right: 14px;"></i>' + ' ' + ' Download File';
  tableUpload = $('#uploadTable').DataTable({
    "ajax": "/lsorg/users/fileupload.php",
    "bPaginate": true,
    "bProcessing": true,
    //    "bserverSide": true,
    "pageLength": 10,
    "autoWidth": false,
    "ordering": true,
    "sortable": true,
    "sAjaxDataProp": "",
    "columns": [{
      data: 'id'
    }, {
      data: 'date'
    }, {
      data: 'company'
    }, {
      data: 'user'
    }, {
      data: 'description'
    }, {
      data: 'file',
      "render": function(data, type, row, meta) {
        if (type === 'display') {
          data = '<a href="' + data + '" style="text-decoration: none; color: red;">' + link + '</a>';
        return data;
    "order": [
        1, "desc"
    "columnDefs": [{
      "width": "20%",
      "targets": [1],
    }, {
      "orderable": false,
      "targets": [0]
    }, {
      className: "hide_id",
      "targets": [0]

Hi @poorbaldrick, try adding a download attribute to the anchor element – it’s not supported by IE though (see here for details). Other than that the actual behaviour also depends on the individual user agent settings.

Hello m3
Thanks for the reply. What happens if a user is not using a browser that is html5? is there a fallback. Thanks

All major browsers support HTML5, and those that don’t (say for some obscure reason you are browsing the web with IE6) still fall back to standards mode. So it is more a question of feature support, and in this case I am not aware of any fallback.

Well the ‘fallback’ would be the default behavior for clicking a link, which is a link that points to a file. So a non-HTML5 browser would attempt to open the link, find it to be a file rather than an HTML transmission, and download the file in the standard manner (as the OP’s link currently operates).

The same thing that happens if you stick wookwook=“blarfinger” into your link. The browser simply doesnt know how to interpret the attribute, so it ignores it.

1 Like

Thanks for all your help with this question guys.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.