0

I have written below lines of code.

for (var counterOfCerticateLoop in certificateDetailsArray) {
    var document_type_id = certificateDetailsArray[counterOfCerticateLoop]['document_type_id'];
    var document_actual_name = certificateDetailsArray[counterOfCerticateLoop]['document_actual_name'];
    var document_unique_name = certificateDetailsArray[counterOfCerticateLoop]['document_unique_name'];
    var document_type = certificateDetailsArray[counterOfCerticateLoop]['document_type'];
    var document_description = certificateDetailsArray[counterOfCerticateLoop]['document_description'];

    var fileURL = '../student/certificates/' + document_unique_name;

    var EditButton = '<button title="Edit" onclick="editContactDetail(\'' + document_type_id + '\')" class="btn btn-primary table_btn btn-outline btn-sm"><i class="fa fa-pencil"></i></button>';

    var DeleteButton = '<button title="Delete" onclick="deleteContactDetail(\'' + document_type_id + '\')" class="btn btn-danger  table_btn btn-outline btn-sm"><i class="fa fa-trash"></i></button>';

    var tableRow = '<tr><td>' + document_type + '</td>' +
        '<td><a href=' + fileURL + '>' + document_actual_name + '</a></td>' +
        '<td>' + document_description + '</td>' +
        '<td>' + EditButton + DeleteButton + '</td>';
    $("#certificateTable").append(tableRow);
}

The above code creates table perfectly. Now the issue is that we click on file link, it does not get downloaded. I want that it should download the file in fileURL which is rendered in anchor tag's href attribute.

Please help !!!

4b0
  • 21,981
  • 30
  • 95
  • 142
Nida Amin
  • 735
  • 1
  • 8
  • 28

2 Answers2

1

Use HTML5 download attribute.

<a download href=' + fileURL + '>' + document_actual_name + '</a>

Note: Be aware may be all version of different browser not supported this attribute

4b0
  • 21,981
  • 30
  • 95
  • 142
0

You can put this code in your .htaccess file and try if it works?

AddType application/octet-stream .csv
AddType application/octet-stream .xls
AddType application/octet-stream .doc
AddType application/octet-stream .avi
AddType application/octet-stream .mpg
AddType application/octet-stream .mov
AddType application/octet-stream .pdf
SRK
  • 3,476
  • 1
  • 9
  • 23