javascript - FormData in IE8/9 -
i have implemented script uploading files ajax, works perfect in other browsers except explorer, noticed formdata not supported ie9 , less, there alternatives formdata in ie, , want use clean javascript
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); (var = 0; < file_input.files.length; ++i) { frm_data.append(file, file_input.files[i]); } } req.upload.addeventlistener('progress', function(e) { //event called while upload 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 completed $('#' + progress).html('<font>retrieving updated data...</font>'); }); req.upload.addeventlistener('error', function(e) { //event called when error returned 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 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); }}
formdata in ie supported ie10, after doing research best solution (my opinion) using ajaxform: http://malsup.com/jquery/form/ works perfect in ie8/9, not sure ie7
Comments
Post a Comment