0

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 .

  var file = document.getElementById('documentfile').files[0];
                var filename = document.getElementById('documentfile').files[0].name;
                var size = document.getElementById('documentfile').files[0].size;
                var ext = $('#documentfile').val().split('.').pop().toLowerCase();
                var reader = new FileReader();
                var fileurl;
                reader.onload = function (e) {
                    fileurl = e.target.result;
                }
                reader.readAsDataURL(file);
                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>'
                $('.document-table-str .table .tbody').append(str);
                $('.document-table-str').show();
                $('#documentfile').val("")
Ajay
  • 1
  • 1
  • Wait what? you want to upload the file without actually uploading it to your server? – flx Sep 10 '18 at 05:29
  • yes, Actually I want to give preview option.user can select multiple file, if the user has doubt on their file, he or she download file and check. after final submit I will upload all files to the server – Ajay Sep 10 '18 at 05:35
  • I have updated code – Ajay Sep 10 '18 at 06:10
  • Better... So your problem is well covered in many dupes all pointing to [this canonical](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). I can't vote to close anymore since I already did against the first rev. But actually, you don't even need to run in this callback nightmare, simply use the synchronous `URL.createObjectURL(file)`. Way better in all ways. And I'm not here to judge or anything, but are you sure you want to make your user download the files they just uploaded from their disk? – Kaiido Sep 10 '18 at 06:24

0 Answers0