php 如何使 new FormData() 在 IE 浏览器上工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15756788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:53:44  来源:igfitidea点击:

how to make new FormData() work on IE browsers

phpjqueryajax

提问by Akhilraj N S

How can I make this work on 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 上不起作用,IE 浏览器不支持 new FormData() api,在 IE 中是否还有其他与 new FormData() 等效的 api?

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);  
    }
});

采纳答案by Akhilraj N S

Its better to use jquery form Js for submitting images over ajax. I found it better than FormData()

最好使用 jquery 表单 Js 通过 ajax 提交图像。我发现它比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;
    });

回答by Klingstedt

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

邮件程序

<!--[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

表单.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 发件人!

回答by Saram

AFAIK it is possible in IE9+ only. To upload file 'ajax like' you should use iframe trick for that. I used that as source when implementing it:

AFAIK 仅在 IE9+ 中是可能的。要上传文件'ajax like',您应该使用 iframe 技巧。我在实现它时使用它作为源:

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

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

回答by thumber nirmal

Apparently, FormData is not supported in IE. You may, however, be able to use jQuery's serialize like so:

显然,IE 不支持 FormData。但是,您可以像这样使用 jQuery 的序列化:

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