2013-03-18 14 views
19

Tôi đã triển khai tập lệnh này để tải tệp lên bằng ajax, nó hoạt động hoàn hảo trong các trình duyệt khác ngoại trừ trình thám hiểm, tôi nhận thấy rằng formData không được IE9 hỗ trợ và ít hơn, có bất kỳ lựa chọn thay thế nào cho formData trong IE, và tôi muốn sử dụng javascript sạchFormData trong IE8/9

function doObjUploadExplorer(url, lnk_id, file, progress, success, content, frm, div_dlg, start_func){ 
    var file_input = null, 
     frm_data = new FormData(), 
     req; 

    try { 
     //firefox, chrome, safari etc 
     req = new XMLHttpRequest(); 
    } 

    catch (e) { 
     // Internet Explorer Browsers 
     req = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 


if (document.getElementById(file)) { 
    file_input = document.getElementById(file); 

    for (var i = 0; i < file_input.files.length; ++i) { 
     frm_data.append(file, file_input.files[i]); 
    } 
} 

req.upload.addEventListener('progress', function(e) { //Event called while upload is in progress 
    if (progress !== undefined 
      && e.lengthComputable) { 
     $('#' + progress).html('<font>Uploading... ' + Math.round((e.loaded/e.total) * 100) + '%</font>'); 
    } 
}); 

req.upload.addEventListener('load', function(e) { //Event called when upload is completed 
    $('#' + progress).html('<font>Retrieving updated data...</font>'); 
}); 

req.upload.addEventListener('error', function(e) { //Event called when an error is returned by the server 
    alert('An error has occurred...'); 
}); 

req.addEventListener('readystatechange', function(e) {   
    if (this.readyState === 4) { 
     if (this.status === 200) { 
      if (content !== undefined) { 
       $('#' + content).html(this.response); 
      } 

      if (success !== undefined) { 
       showChkMark(success); 
      } 
     } else { 
      console.log('Server replied with HTTP status: ' + this.status); 
     } 


     if (progress !== undefined) { 
      $('#' + progress).hide(); 
     } 

     if (div_dlg !== undefined) { 
      $('#' + div_dlg).dialog('close'); 
     } 

     $('#' + file) 
     .attr('disabled', false) 
     .val(''); 
    } 
}); 

if (progress !== undefined) { 
    $('#' + progress).show(); 
} 

$('#' + file).attr('disabled', true); 
url += (
     url.indexOf('?') === -1 
     ? '?' 
     : '&' 
    ); 
url += 'lnk_id=' + lnk_id + '&file=' + file; 
req.open('POST', url); 
req.setRequestHeader('Cache-Control', 'no-cache'); 

if (start_func !== undefined) { 
    start_func.apply(); 
    setTimeout(function() { 
     req.send(frm_data); 
    }, 500); 
} else { 
    req.send(frm_data); 
}} 
+0

Tôi không nghĩ có bất kỳ phương án thay thế nào khác –

+0

Kiểm tra bài đăng tương tự này, không có cách nào thực sự vòng này. http://stackoverflow.com/questions/7899513/send-multipart-formdata-with-jquery-ajax-in-ie –

+0

[Tôi phải đối mặt với vấn đề tương tự với các tập tin [0] và FormData. Giải pháp này làm việc cho tôi] (https://stackoverflow.com/questions/26206105/formdata-in-ie-11-not-defined) – SJ03

Trả lời

19

FormData trong IE chỉ được hỗ trợ từ IE10, sau khi làm một số nghiên cứu giải pháp tốt nhất cho việc này (quan điểm của tôi) sẽ được sử dụng ajaxForm: http://malsup.com/jquery/form/ hoạt động hoàn hảo trong IE8/9, không chắc chắn về IE7

+11

Hoạt động hoàn hảo, nếu bạn có một plugin tốt hơn, xin vui lòng tư vấn ... – Lappies

+0

nào tốt hơn plugin bạn đã sử dụng sau đó? –

+2

Liên kết của bạn không liên quan gì đến bất kỳ ajaxForm nào khác ngoài 'jquery-upload-progress'. – ProfK