Download HTML link from the bank

Asked

Viewed 1,304 times

5

I am recording an address in the bank and with ajax I search it for display to the user. Provisionally, I am recording it in 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 all:

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

inserir a descrição da imagem aqui

My question is: How could this become a hyperlink or download button? I tested button, onclick, but he couldn’t recognize the link. Thank you

UPDATE

The Ajax that loads the data is this:

       // 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 by 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>
  • Tried to insert the url into a hyperlink using the tag <a>?

  • I don’t know how to do this, because Ajax feeds the field by its id, on the page the value appears correctly, but as text only.

  • You are using jQuery or some other framework or pure Javascript?

  • I am using pure JS. I tried the answer below but gave Ajax error...

3 answers

5

Instead of putting the file path in an id put in href and use the download attribute, 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>
<a href="" download="DownloadMyJavascriptBookt">Download</a>

  • 1

    Remember that in more recent versions of jQuery the attr was replaced by prop. +1

  • I tried, but ajax does not load any of the other data and firebug is empty. Thank you

  • @Diego in your image you are using an input, use a <a> tag, if you have files that can be opened in the browser, for example pdf, open it in a new tab using target="_Blank"

  • @Gabrielrodrigues tried to use the <a> tag, but it returns nothing.

3

First you must use a hyperlink tag <a> and using Javascript you will add the URL to the attribute href using the function setAttribute.

Take the example:

var $meuLink = document.getElementById('meuLInk');
var minhaUrl = '/';
$meuLink.setAttribute('href', minhaUrl);
<a id="meuLInk">Link</a>

  • The url is not fixed, I am trying to adapt this code to get the value of the input when it is stored.

  • Just assign the value in your code, at the time you recover via AJAX.

  • I edited the question displaying Ajax, because I didn’t understand how I could change it. Thanks for the help.

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, the download window opens. Thank you all for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.