jquery插件ajaxupload实现文件上传操作

2020-05-29 07:11:33易采站长站整理

// on form submit will open
// var iframe = document.createElement('iframe');
// iframe.setAttribute('name', id);

var iframe = toElement('<iframe src="javascript:false;" name="' + id + '" />');
// src="javascript:false; was added
// because it possibly removes ie6 prompt
// "This page contains both secure and nonsecure items"
// Anyway, it doesn't do any harm.
iframe.setAttribute('id', id);

iframe.style.display = 'none';
document.body.appendChild(iframe);

return iframe;
},
/**
* Creates form, that will be submitted to iframe
* @param {Element} iframe Where to submit
* @return {Element} form
*/
_createForm: function (iframe) {
var settings = this._settings;

// We can't use the following code in IE6
// var form = document.createElement('form');
// form.setAttribute('method', 'post');
// form.setAttribute('enctype', 'multipart/form-data');
// Because in this case file won't be attached to request
var form = toElement('<form method="post" enctype="multipart/form-data"></form>');

form.setAttribute('action', settings.action);
form.setAttribute('target', iframe.name);
form.style.display = 'none';
document.body.appendChild(form);

// Create hidden input element for each data key
for (var prop in settings.data) {
if (settings.data.hasOwnProperty(prop)) {
var el = document.createElement("input");
el.setAttribute('type', 'hidden');
el.setAttribute('name', prop);
el.setAttribute('value', settings.data[prop]);
form.appendChild(el);
}
}
return form;
},
/**
* Gets response from iframe and fires onComplete event when ready
* @param iframe
* @param file Filename to use in onComplete callback
*/
_getResponse: function (iframe, file) {
// getting response
var toDeleteFlag = false,
self = this,
settings = this._settings;

addEvent(iframe, 'load', function () {

if ( // For Safari
iframe.src == "javascript:'%3Chtml%3E%3C/html%3E';" ||
// For FF, IE
iframe.src == "javascript:'<html></html>';") {
// First time around, do not delete.
// We reload to blank page, so that reloading main page
// does not re-submit the post.

if (toDeleteFlag) {
// Fix busy state in FF3
setTimeout(function () {
removeNode(iframe);
},
0);
}

return;
}

var doc = iframe.contentDocument ? iframe.contentDocument : window.frames[iframe.id].document;

// fixing Opera 9.26,10.00
if (doc.readyState && doc.readyState != 'complete') {
// Opera fires load event multiple times
// Even when the DOM is not ready yet