Downloadable HTML link coming from the bank

5

I'm writing an address in the database and with ajax I'm looking for it to display to the user. Temporarily, I'm writing it to the input below:

              <tr>
              <td>Download:</td>
              <td><input type="text" name="anexo" id = "anexo" size="60"></td>
              </tr>

Ajax returns the correct value of the database, with HTTP and everything:

http://pcn-sig.peccin.local/sig/subsistemas/projeto_testes/projetos/BASE/11.docx

Myquestionis:Howcouldthismakeitahyperlinkordownloadbutton?Itriedbutton,onclick,butIcouldnotrecognizethelink.Thanks

UPDATE

TheAjaxthatloadsthedataisthis:

       // Verifica se foi concluído com sucesso e a conexão fechada (readyState=4)
        if (xmlreq.readyState == 4) {
            // Verifica se o arquivo foi encontrado com sucesso
            if (xmlreq.status == 200) {
                //Se o retorno foi vazio do Oracle
                if (xmlreq.responseText == "") {
                    document.getElementById("projeto").focus();
                    alert("Não existe o projeto informado!");
                    ids.forEach(function (id) {
                        document.getElementById(id).value = '';
                        document.getElementById("projeto").value = '';
                    });
                //Se encontrou dados
                } else {
                    //Aqui recebe os dados do processa.php, abre e aplica nos campos desejados
                    var dados = JSON.parse(xmlreq.responseText);
                    // função para preencher os campos com os dados
                    ids.forEach(function (id) {
                        document.getElementById(id).value = dados[id];
                    });
                }
            } else {
                result.innerHTML = "Erro: " + xmlreq.statusText;
            }
        }

UPDATE 2

Below I was able to open a new window when clicking, but instead of passing the url it passes this.value

              <td>Download:</td>
              <td><input type="text" name="anexo" id = "anexo" onclick="location.href='this.value';" /></td>
    
asked by anonymous 13.01.2016 / 20:41

3 answers

3

Resolved as follows:

<td><input type="text" name="anexo" id = "anexo" size = "65" readonly = "true" onclick="location.href=this.value;" /></td>

When I click on the field it will open the download window. Thank you all for the help.

    
14.01.2016 / 11:23
5

Instead of putting the path of the file into an id put it in href and use the download attribute, for example:

var arquivoParaDownload = "https://www.novatec.com.br/livros/javascriptguia/capitulo9788575222485.pdf";

$("a").prop('href', arquivoParaDownload); // Para jQuery =< 1.6 use attr()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script><ahref="" download="DownloadMyJavascriptBookt">Download</a>
    
13.01.2016 / 20:50
3

First you should use a hyperlink tag <a> and using JavaScript you will add the URL in the href using the setAttribute function.

See the example:

var $meuLink = document.getElementById('meuLInk');
var minhaUrl = 'http://pt.stackoverflow.com/';
$meuLink.setAttribute('href', minhaUrl);
<a id="meuLInk">Link</a>
    
13.01.2016 / 20:59