0

in the below code iframe is using to upload file to server. see the code and tell me when iframe load event is fired ?

<script type="text/javascript">
    $(document).ready(function () {

        $("#formsubmit").click(function () {

            var iframe = $('<iframe name="postiframe" id="postiframe" style="display: none"></iframe>');

            $("body").append(iframe);

            var form = $('#theuploadform');
            form.attr("action", "/upload.aspx");
            form.attr("method", "post");

            form.attr("encoding", "multipart/form-data");
            form.attr("enctype", "multipart/form-data");

            form.attr("target", "postiframe");
            form.attr("file", $('#userfile').val());
            form.submit();

            $("#postiframe").load(function () {
                iframeContents = this.contentWindow.document.body.innerHTML;
                $("#textarea").html(iframeContents);
            });

            return false;

        });

    });

</script>

code taken from here jQuery iframe file upload

the above code dynamically creating iframe and append to body and submitting the form. how the above code file is uploading to server by iframe not clear?

what is relation between file upload and iframe in above code ?

when iframe load will call ?

$("#postiframe").load(function () {
                iframeContents = this.contentWindow.document.body.innerHTML;
                $("#textarea").html(iframeContents);
            });

see the code

$("#postiframe").load(function () {
                iframeContents = this.contentWindow.document.body.innerHTML;
                $("#textarea").html(iframeContents);
            });

what this line is doing iframeContents = this.contentWindow.document.body.innerHTML; ?

why the below code is required.

$("#postiframe").load(function () {
                iframeContents = this.contentWindow.document.body.innerHTML;
                $("#textarea").html(iframeContents);
            });

if we remove the above code then form will be submitted also then when we try to call load event of iframe.......bit confuse.

please discuss in details to understand it properly. thanks

thanks

Community
  • 1
  • 1
Mou
  • 15,673
  • 43
  • 156
  • 275

0 Answers0