4
I want to force the download of a file and I’m trying these two ways.
downloading on the same page:
var docLocation = window.location.href + 'docs/apresentacao.pdf';
var iframe = $('<iframe src="' + docLocation + '" class="hidden"></iframe>');
$('body').append(iframe);
*so the browser tries to interpret the file and returns the message:
Resource Interpreted as Document but transferred with MIME type application/pdf
I saw somewhere saying to convert the MIME type to application/octet-stream
with a back-end language and this would force the download.
downloading in new window:
window.open(docLocation, '_blank');
The problem with this method is that it opens the file in a new window (pop-up), and since browsers block pop-ups by default, the user would have to allow loading of this window which would eventually cause some users not to see.
I always found the solution of the download attribute perfect for these cases, but there is the problem of some browser support. The ideal would be to create a Servlet.
– Matheus
If it’s not compatible, you can do a function that for example, gives an Alert saying "Right click to download the file", @Matheus
– Miguel
Prior search to avoid duplicates is fundamental for the community: >>>>>>>>>> http://answall.com/a/15842/3635 e >>>>>>>>>>>> http://answall.com/a/13580/3635
– Guilherme Nascimento
@Guilhermenascimento sorry, I really did not notice, I confess that I do not have the habit yet. I will be more attentive to this
– Miguel