如何在IE8中使用Ajax将图像发送到.net Webservice

How to send image to .net Webservice using Ajax in IE8?

本文关键字:图像 Webservice net Ajax IE8      更新时间:2023-09-26

下面的帖子是有关:如何使用Ajax发送图像到PHP文件?

我设法让这个工作按照上面的帖子,但它无法在IE8上工作。

是否有办法让这个工作在ie8+?

下面是我的代码:
$("form[name='uploader']").submit(function(e) {
    var formData = new FormData($(this)[0]);
    $.ajax({
        url: dotnetpage,
        type: "POST",
        data: formData,
        async: false,
        success: function (msg) {
            $('.js-ugc-image').attr('src', msg);
        },
        cache: false,
        contentType: false,
        processData: false
    });
    e.preventDefault();
});

IE 8没有formdata,您可以使用隐藏的iframe并发布它并读取结果。我用了一个类似这样的技巧克隆表单,将原始表单移动到一个隐藏的iframe中(这需要这样做,因为你不能在IE上克隆或设置输入类型文件的值),然后提交并读取提交的结果。

类似这样的代码,这是我以前使用过的,并且有效:

var $form = $('your form');//GET YOUR FORM
//Create Hidden iframe
var _hiddenIframe = $('<iframe id="_hiddenframe" style="display:none;"></iframe>');
//Create Copy Form and add the attributes of the original
var _copyForm = $('<form id="_copyForm" name="_copyForm" style="">');
_copyForm.attr({'method':$form.attr('method'),'action':$form.attr('action'), 'enctype':$form.attr('enctype')});             
//Get original fields
$original = $form.children('*');
//Clone and append to form
$original.clone(true).appendTo($form);
//send the original fields to hidden form
$original.appendTo(_copyForm);  
//Add the iframe to the body
_hiddenIframe.appendTo('body');
//Add the form to the hidden iframe 
_copyForm.appendTo(_hiddenIframe.contents().find('body'));
var $r; 
//submit the form
_copyForm.submit();
//after it reloaded(after post)
_hiddenIframe.on('load',function(){ 
    //read result (maybe a json??)
    $r = $.parseJSON(_hiddenIframe.contents().find('body').text());
    //Do something with the result
    if($r.result=='ok'){
        //Do Something if ok
    }           
    else{
        //Do Something if error
    }
}); 

不,对不起,IE8不支持FormData对象。(见http://caniuse.com/搜索= formdata)

你可以将<input type='file >标签嵌入到一个单独的表单中,然后使用jQuery提交。