0

I have HTML page that presents user and password, and on load of the page i want it to sent GET request to my local python code with vars and my function should return the downloadable file.

This is my code:

<!DOCTYPE html>
<html>
<body>
<p style="font-family:verdana"> <strong>Username:</strong>  <span class="js-content">{{user}}</span> </p>
<button class="js-copy">Copy</button>
<hr>
<p style="font-family:verdana"><strong>Password:</strong> <span class="js-content">{{pass}}</span> </p>
<button class="js-copy">Copy</button>

<input type=hidden name="randomVal" value="{{ randomVal }}">
<input type=hidden name="output" value="{{output}}">

<script>

(function(){

/* Creating textarea only once, but not each time */
let area = document.createElement('textarea');
document.body.appendChild( area );
area.style.display = "none";

let content = document.querySelectorAll('.js-content');
let copy    = document.querySelectorAll('.js-copy');

for( let i = 0; i < copy.length; i++ ){
  copy[i].addEventListener('click', function(){
    area.style.display = "block";
    /* because the classes are paired, we can use the [i] index from the clicked button,
    to get the required text block */
    area.value = content[i].innerText;
    area.select();
    document.execCommand('copy');
    area.style.display = "none";

    /* decorative part */
    this.innerHTML = '<span style="color: red;">Copied</span>';
    /* arrow function doesn't modify 'this', here it's still the clicked button */
    setTimeout( () => this.innerHTML = "Copy", 2000 );
  });
}

})();

</script>
</body>
</html>

Now how can i make that before it loads \ on load it sends a get request like :

<form method="get" action="/pythonRouteName" style="display: inline;">

(Form will not work for me as it redirects.) And sending this values:

<input type=hidden name="randomVal" value="{{ randomVal }}">
<input type=hidden name="output" value="{{output}}">

With the get request but without moving \ redirecting to this get request.

I want to stay on the same page that shows user and password,

There are a lot of sites that does this - you enter and automatically it download some file but you stay on the same page.

EDIT: Things i tried- added this function :

    function startDownload("download_function") {
    window.location.href = "download_function";
    <input type=hidden name="randomVal" value="{{ randomVal}}">
    <input type=hidden name="output" value="{{output}}">>

}
window.onload = startDownload();
</script>

It doesn't do anything. i keep looking for a solution

gsr
  • 169
  • 2
  • 19
  • Prevent the form from submitting and send an AJAX request to your server. Take the response and process it on the client side – empiric May 12 '20 at 14:28
  • 1
    _“There are a lot of sites that does this”_ - guess what, there are also a lot of places where this has been explained already … you just need to do a bit of proper research. https://stackoverflow.com/questions/2923262/trigger-file-download-on-a-page-with-content – CBroe May 12 '20 at 14:28
  • https://stackoverflow.com/questions/12365534/launch-download-in-the-same-tab-without-opening-new-tab-or-window-in-javascript – CBroe May 12 '20 at 14:29
  • https://stackoverflow.com/questions/604806/how-do-you-serve-a-file-without-leaving-the-page – CBroe May 12 '20 at 14:30
  • https://stackoverflow.com/questions/13065697/redirect-show-view-after-generated-file-is-dowloaded – CBroe May 12 '20 at 14:30
  • 1
    If you actually made an effort to look stuff up beforehand, then your question should reflect that. Then I would expect to read something like, _“so, i wanted to do X, my research lead me to this, I tried to implement it, but [proper problem desciption here.]”_ – CBroe May 12 '20 at 14:45
  • @CBroe so i need to post all the things i tried and didnt work ? the links you added shows how to redirect without the input that i need. everything i found are or with a button or only redirects. – gsr May 12 '20 at 14:55

0 Answers0