0

I need to download a file from a link I am given. In order to do this I must make a get request to that link. It can have 3 states:
1. Code 200 and the download will begin once the request landed
2. Code 202 which means I must repeat the request because the file is being uploaded
3. Error code and I must create a dom element that shows that.

How it works:
I make a request to this rails action:

def by_month
    export_form = Commissions::ByMonthForm.new(current_user)
    if export_form.submit(params)
      @export = export_form.export
    else
      show_errors export_form.errors
    end
  end 

This in turn starts the file upload. Which I don't know when it's ready(depending on how big the file it is). Now I must create a javascript get request to a link that follows the indications I have given at the beginning of the post. And integrate it in the by_month.html.erb view from rails . The javascript I managed to write is:

function httpGetAsync(theUrl){
      var xmlHttp = new XMLHttpRequest();
      xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState == 4) {
          if (xmlHttp.status == 200) {
            redirect_to_main();
          }
          else if(xmlHttp.status == 202) {
            httpGetAsync(theUrl);   
          }
          else {
            make_error_css();
          }
        }
      }
      xmlHttp.open("GET", theUrl, true); // true for asynchronous
      xmlHttp.send(null);
    }

However I don't think it works. Any ideas of how I can do this?(redirect_to_main and make_error_css are functions that I will implement myself later).

1 Answer 1

2

Update As per the comments below

Can you try this,

function httpGetAsync(theUrl){
      var xmlHttp = new XMLHttpRequest();
      xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState == 4) {
          if (xmlHttp.status == 200) {
            redirect_to_main();
          }
          else if(xmlHttp.status == 202) {
           setTimeout(
            makeRequest(theUrl),
             3000);   
          }
          else {
            make_error_css();
          }
        }
      }
      //makeRequest(xmlHttp, theUrl);
      xmlHttp.open("GET", theUrl, true); // true for asynchronous
      xmlHttp.send(null);

    }

function makeRequest(theUrl){
           httpGetAsync(theUrl);
        }

makeRequest() is where the request is made again if the status is 202.

Sign up to request clarification or add additional context in comments.

4 Comments

but if the status is again 202 I need to remake it(there can be an undefinite number of 202 answers since I am waiting for a document download, I need to repeat this call every 3 seconds or something
So from what i understand is, if the status is 202 (doesn't matter how many times), each time you got to wait for 3 seconds and make a fresh call again? Is my understanding correct?
function makeRequest(theUrl) { setTimeout(httpGetAsync(theUrl), 3000); }; something like this :D
hi @LucianTarna I have made some changes, see if this answers your concerns :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.