简体   繁体   English

如何使新的FormData()在IE浏览器上运行

[英]how to make new FormData() work on IE browsers

How can I make this work on IE? 如何在IE上完成这项工作? This won't work on IE, new FormData() api is not supported by IE browsers, is there any other api equivalent to new FormData() in IE? 这不适用于IE浏览器,IE浏览器不支持新的FormData()API,是否还有其他api等同于IE中的新FormData()?

var fd = new FormData();
fd.append( "userfile", $("#userfile")[0].files[0]);

$.ajax({
    url : '/user/ajax_upload/',
    type: 'POST',
    contentType:false,
    cache: false,
    data: fd,
    processData: false,
    beforeSend :function(){
    },
    success : function( data ) {
        $('#popupbox').html(data);  
    }
});

Its better to use jquery form Js for submitting images over ajax. 最好使用jquery形式Js通过ajax提交图像。 I found it better than FormData() 我发现它比FormData()更好

<script type="text/javascript" src="/js/jquery.form.js"></script>

function update_professional_details(){
    var options = { 
                url     : '/validateform/personal',
                type    : $("#personal_edit_form").attr('method'),
                dataType: 'json',
                success:function( data ) {
                    var msg = data.msg;
                    if(data.status == 'success'){
                        $("#msg_data").html("Updated successfully, redirecting...")
                        $("#personal_edit_form").submit();
                    }else{
                        $('p[class$="_error2"]').html('');
                        var msg = data.msg;
                        $.each(msg, function(k, v) {
                            $('.'+k+'_error2').html(v);
                        });
                    }
                },
            }; 
            $('#personal_edit_form').ajaxSubmit(options);
                return false;
        }

    $('#updatepersonal').click(function(){
        update_professional_details();
            return false;
    });

Actually i made an alteration to my code to be able to use $.ajax in all other browsers and just made an iframe for the IE browsers like this. 实际上我对我的代码进行了更改,以便能够在所有其他浏览器中使用$ .ajax,并为这样的IE浏览器制作了iframe。

mailer.php mailer.php

<!--[if IE]>
   <iframe src="form.php"></iframe>
<![endif]-->

<![if !IE]>
<script>
    $(document).ready( function() {
        //Program a custom submit function for the form
        $("#form").submit(function(event){

          //disable the default form submission
          event.preventDefault();

          //grab all form data  
          var formData = new FormData($(this)[0]);

          $.ajax({
            url: $("#form").attr('action'),
            type: 'POST',
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (returndata) {
              alert(returndata);
            }
          });

          return false;
        });
    });
</script>

<?php include_once ('form.php'); ?>

<div id="email-success"></div>
<![endif]>

form.php form.php的

<form id="form" action="form-exec.php" target="_self" method="post" enctype="multipart/form-data">
    <input type="text" name="email-to" value="" />
    <input type="text" name="email-subject" value="" />
    <input type="text" name="email-message" value="" />
    <input type="file" name="file" />
    <input type="file" name="file2" />
    <button type="submit" name="email-send">Skicka</button>
</form>

and the form-exec.php is, in my case, my PHPmailer sender! 在我的例子中,form-exec.php是我的PHPmailer发件人!

AFAIK it is possible in IE9+ only. AFAIK只能在IE9 +中使用。 To upload file 'ajax like' you should use iframe trick for that. 要上传文件'ajax like',你应该使用iframe技巧。 I used that as source when implementing it: 我在实现它时使用它作为源:

http://ramui.com/articles/ajax-file-upload-using-iframe.html http://ramui.com/articles/ajax-file-upload-using-iframe.html

Apparently, FormData is not supported in IE. 显然,IE中不支持FormData。 You may, however, be able to use jQuery's serialize like so: 但是,您可以像这样使用jQuery的序列化:

        var FD = $('form').serialize();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM