Ajay Gupta

Ajay Gupta

  • NA
  • 330
  • 59.2k

Download selected file as preview option using Jquery

Sep 10 2018 1:33 AM
In my case the user can select a file and click on the submit button. after submitting button I am showing the file as table format with file name, file size, download link and remove option. now my requirement is if user click on the download button from the table then the file should download. I don't want to upload file server side on a selection of files .
 
  1. var file = document.getElementById('documentfile').files[0];  
  2.                var filename = document.getElementById('documentfile').files[0].name;  
  3.                var size = document.getElementById('documentfile').files[0].size;  
  4.                var ext = $('#documentfile').val().split('.').pop().toLowerCase();  
  5.                var reader = new FileReader();  
  6.                var fileurl;  
  7.                reader.onload = function (e) {  
  8.                    fileurl = e.target.result;  
  9.                }  
  10.                reader.readAsDataURL(file);  
  11.                var str = '<tr><td>1</td><td>' + filename + '</td><td>' + $('.sltdoctype').val() + '</td><td>' + niceBytes(size) + ' </td><td> <a href=' + fileurl + '  download>download</a></td><td> <a href="#" class="anchorRemove" >Remove</a> </td></tr>'  
  12.                $('.document-table-str .table .tbody').append(str);  
  13.                $('.document-table-str').show();  
  14.                $('#documentfile').val("")  

Answers (2)