Amazon S3 upload via Iframes

后端 未结 4 1811
北荒
北荒 2021-02-01 05:16

Sigh, we\'re back to this. I can easily enough use CORS on any decent enough browser to directly upload files to my AWS S3 bucket. But (it was coming), with IE I have to fall ba

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-01 06:09

    AS for the "postMessage" scenario, maybe the iframe should contain a simple javascript

    [edit] for iframes taken over by an errormessage

    IFRAME script

    window.document.onload = function(e){ 
        window.parent.postMessage(document, '*'); //replace '*' with your parent if possible   
    }
    // just to get the proper document for the parent to target me
    window.addEventListener('message',function(e) {
        if (e.domain == 'example.com') { // the domain of your parent frame
            if (e.data == "Salute") {
                window.parent.postMessage("I'm here", '*'); //replace '*' with your parent too
            }
        }
    });
    

    Now parent knows the iFrame perfectly well and can track it's status (depending on if it's answering a simple postMessage)

    PARENT script

    var iFrameTarget;
    var iFrameTakenOver = false;
    var timer;
    window.addEventListener('message',function(e) {
        if (e.domain == 'example.com') { // the domain of your iframe
            if (e.data) { // e.data contains the iframe document
                if(typeof(e.data) =='object')
                    iFrameTarget = e.source;
                elseif(e.data == "I'm here")
                {
                    iFrameTakenOver = false;
                }
                timer =setInterval(call_iFrame(),5000); // check iFrame presence in 5 seconds
            }
        }
    });
    
    function call_iFrame() {
        iFrameTarget.postMessage('Salute');
        iFrameTakenOver = true;
    }
    

    IF iframe is not responding with it's "code" iFrameTakenOver will be permanently set to false checking that will verify if an error has occured or not.

提交回复
热议问题