How to download an image from Angularjs

Asked

Viewed 109 times

0

I’m creating a feature similar to the email attachment, in which we attach files but if we want to view it to confirm the files to be sent we download.

That’s where I’m having the problem

function abrirArquivo() {

var decoded = $base64.decode(base64);

var url = window.URL.createObjectURL(new Blob([decoded], {type:"image/png"}));

var link = document.createElement('a');

link.href = url;

link.setAttribute('download', "teste");

document.body.appendChild(link);

link.click();

}

I use the function above that picks up the base64 from the image I want to download, converts to binary and passes to the constructor of the Blob, but when the download ends the image opens with error:

inserir a descrição da imagem aqui

1 answer

0

The solution:

function mostrarArquivo(arquivo) {
        Upload.base64DataUrl(arquivo)
            .then(function(response) {
                baixarArquivo("imagem", response);
            });
    }

    function baixarArquivo(nomeArquivo, base64) {
        var element = document.createElement('a');
        element.setAttribute('href', base64);
        element.setAttribute('download', nomeArquivo);
        element.style.display = 'none';
        document.body.appendChild(element);
        element.click();
        document.body.removeChild(element);
    }

Browser other questions tagged

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