Provide downloadable file via javaScript

4

I have a javascript application where the user can see certain files from the server. I would like to know how to make these files available for download, something like a "Save As" button. Thank you in advance.

    
asked by anonymous 14.07.2015 / 23:03

2 answers

1

Hello, is this good?

The download attribute downloads the file that is in href .

function mudarLink() {
  var baixar = document.getElementById('baixar');
  baixar.href = "http://cdn.sstatic.net/img/share-sprite-new.svg?v=24e64812c790";
  baixar.innerHTML = "Baixar imagem Stack Exchange";
}
<button onclick="mudarLink()">Mude o Link</button>
<a id="baixar" href="http://cdn.sstatic.net/br/img/sprites.svg?v=e26b234630f5" download>Baixar imagem Stack Overflow</a>

Edited:

Warning that does not work on all browsers: link

    
14.07.2015 / 23:34
0

My problem was: download after selecting an option I solved like this:

<select id="tipo_documento" name="tipo_documento">
<option value="" disabled="disabled" selected="selected">.:: Modelos de Documentos ::.</option>
<option value="Documentos/DayCamp/Contrato.doc">Contrato.doc</option>
<option value="Documentos/DayCamp/Orçamento.doc">Orçamento.doc</option>
</select>

<a href="#" download id="btnBaixarModelo" name="btnBaixarModelo" class="btn bg-purple form-control pull-right"  title="Baixar Modelo">
<i class="fa fa-cloud-download mao" title="Baixar Modelo"></i>
</a>

$('#frmOrcamento #tipo_documento').on('change',function(){
var url = $(this).val();$('#frmOrcamento #btnBaixarModelo').attr({'href':url});
})

I know that "download" does not work in some browsers, but my client uses chrome and firefox. Business rule: when selecting an option it copies and glues the path to the button link [class .btn = > bootstrap]

Try to create something similar, it is possible that my idea will help you, success!

    
17.10.2017 / 14:53