Iframe.readyState does not work in chrome

后端 未结 4 874
长情又很酷
长情又很酷 2020-11-30 08:17

I create an Iframe on the fly and set as the url a page that downloads a binary file (xls, doc...). While files are downloading I show an animation. When do

相关标签:
4条回答
  • 2020-11-30 08:57

    Sarkiroka's solution worked for me. I have added an instance id to the cookie name to provide a thread safe solution:

    const instanceId = "some uuid retrieved from client";
    
    response.cookie(`fileDownloaded-${instanceId}`,'true', { path: '/', secure: true, maxAge: 90000});
    response.header('attachment','your-file-name.any');
    //...write bytes to response...
    

    client

    var checker = setInterval(()=>{
        if(document.cookie.indexOf(`fileDownloaded-${instanceId}`)>-1){
            alert('done');
            clearInterval(checker);
        }
    },100);
    
    
    0 讨论(0)
  • 2020-11-30 08:58

    Please try this - you are really mixing dom and jQuery from line to line

    var tId;
    
    function stopAnim() {
        // I stop the animation and show the page
        animation.hide();
        progressBar.hide();
        $('#page').show();
        clearInterval(tId);
    }
    var iframe = $("<iframe />");
    iframe.css("visibility","hidden");
    
    iframe.on("readystatechange",function() {
     if (this.readyState == "complete" || this.readyState == "interactive") {
       stopAnim();
     }
    });
    iframe.on("load",function() { // can possibly be deleted
     if (tId) {
       stopAnim();
     }
    });
    
    iframe.attr("src","GetFile.aspx?file=" + fileName);
    $("body").append(iframe);
    tId = setInterval(function() {
      // update progress here
    }, 1000); // 
    
    0 讨论(0)
  • 2020-11-30 09:10

    The downloadable file content doesn't trigger the readystatechange event handler or the onload event handler. This couse you can set a cookie in server side together the file content, and client side check this cookie periodically. For example:

    server

    response.cookie('fileDownloaded','true');
    response.header('attachment','your-file-name.any');
    //...write bytes to response...
    

    client

    var checker = setInterval(()=>{
        if(document.cookie.indexOf('fileDownloaded')>-1){
            alert('done');
            clearInterval(checker);
        }
    },100);
    

    Of course, you can use your framework to check the cookie value correctly, this is just a poc, not a safe cookie parser.

    0 讨论(0)
  • 2020-11-30 09:19

    You can use the onload to signaling the load of the iframe

    here is a simple example that working

    var iframe = document.createElement("iframe");
    iframe.style.display = "none";
    // this function will called when the iframe loaded
    iframe.onload = function (){
      iframe.style.display = "block";    
      alert("loaded");
    };
    // set the src last.
    iframe.src ='http://www.test.com';
    
    // add it to the page.
    document.getElementById("one").appendChild(iframe);
    

    Tested here:
    http://jsfiddle.net/48MQW/5/
    With src loaded last.
    http://jsfiddle.net/48MQW/24/

    0 讨论(0)
提交回复
热议问题