Best Jeanist
Best Jeanist

Reputation: 1129

Remove link after file is downloaded

I have this code below that consists of a img and a capture button. What i have done below is that whenever the user clicks on download it will prepend this link to my img src https://cors-anywhere.herokuapp.com/.

What i'm trying to accomplish is that after the download is launched i want to remove that link https://cors-anywhere.herokuapp.com/ from my img src. Currently i have no idea on how to go about this any help would be greatly appreciated thanks!

function sendData() {
  html2canvas(document.getElementById('capture'), {
    allowTaint: false,
    useCORS: true
  }).then(function(canvas) {
    $('#testaroni').attr('src', function(index, src) {
      return 'https://cors-anywhere.herokuapp.com/' + src;
    });
    $('#test').attr('href', canvas.toDataURL('image/png'));
    $('#test').attr('download', 'Test.png');
    $('#test')[0].click();

  });
}
<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>

<head>
  <meta charset="utf-8" />

  <link rel="shortcut icon" href="//#" />
  <script type="text/javascript" src="https://html2canvas.hertzen.com/dist/html2canvas.js"></script>
  <script type="text/javascript" src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
</head>

<body>
  <div id="capture">
    <div class="jobs-panel">
      <img id="testaroni" data-target="#openly" src="https://cdn.bulbagarden.net/upload/thumb/4/49/Ash_Pikachu.png/250px-Ash_Pikachu.png" width="300" height="300">
    </div>
  </div>
  <button type="button" onclick="sendData()" ;>Capture!</button>
  <a id="test" href="#"></a>
</body>

</html>

Upvotes: 0

Views: 984

Answers (1)

Himanshu Upadhyay
Himanshu Upadhyay

Reputation: 6565

You can do like this:

var flag = true;
function sendData() {
  html2canvas(document.getElementById('capture'), {
    allowTaint: false,
    useCORS: true
  }).then(function(canvas) {
    $('#testaroni').attr('src', function(index, src) {
      return 'https://cors-anywhere.herokuapp.com/' + src;
    });
    if(flag == true)
    {
        $('#test').attr('href', canvas.toDataURL('image/png'));
        $('#test').attr('download', 'Test.png');
        $('#test')[0].click();
        $('#testaroni').attr('src','https://cdn.bulbagarden.net/upload/thumb/4/49/Ash_Pikachu.png/250px-Ash_Pikachu.png');
        flag = false;
    }
  })
}

Upvotes: 1

Related Questions