-1

There are two objectives that I want, 1st is to hide the URL when mouse hover (Success). 2nd is to download the file when <a> is clicked.

Current problem is when I click at the <a> tag, an error appear.

My Reference

What I have tried as below:

  1. console when click at Download

enter image description here

  1. At Inspect > Element:

enter image description here

Code:

$('#myTable').DataTable({
    ...
    columns: [
        { data : "filename",
            render: function (data){
                var test = 'uploads/upload_file/'+filename;
                console.log(test); // I got uploads/upload_file/TEST 1.pdf
                return "<a href='javascript:void(0)' onclick='location.href="+test+"'>Download</a>"
            }
        },
    ],
});
mastersuse
  • 936
  • 1
  • 15
  • 35

1 Answers1

0

Finally, I solved by try this solution.

THE ANSWER

  1. Create a php file called file_download.php

header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.$_REQUEST['f']); readfile('uploads/upload_file/'.$_REQUEST['f']); exit;

  1. Put this in jQuery function

function downloadFile(filename){ window.location.href = 'file_download.php?f='+ filename; }

E_net4
  • 27,810
  • 13
  • 101
  • 139
mastersuse
  • 936
  • 1
  • 15
  • 35