繁体   English   中英

如何在IE8中使用Ajax将图像发送到.net Web服务?

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

以下帖子与以下内容有关: 如何使用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/#search=formdata

您可以做的就是将<input type='file >标记嵌入单独的表单中,然后使用jQuery提交。

暂无
暂无

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

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