function submitFun() {
var files = [document.getElementById('sc')];
ajaxUploadFile({url:'Index.aspx','files':files,success:function(){alert('success');}});
}
//{url, files, data,success,error}
function ajaxUploadFile(ajaxOptions) {
var iframeName = 'tmp_iframe';
var newform, iframe;
var body = document.body;
//伺服器的傳回值
var xml = {};
var url = ajaxOptions.url;
var files = ajaxOptions.files;
var data = ajaxOptions.data;
var success = ajaxOptions.success;
var error = ajaxOptions.error;
//一個對象數組,對象有兩個屬性,一個表示臨時input檔案上傳框,一個表示原始檔案上傳框
var fileArr = [];
//提交完成或者逾時後的回呼函數
var uploadedCallback = function(timeout) {
try {
//如果不是因為逾時才調用此回呼函數
if ('timeout' != timeout) {
if (iframe.contentWindow) {
xml.responseText = iframe.contentWindow.document.body ? iframe.contentWindow.document.body.innerHTML : null;
xml.responseXML = iframe.contentWindow.document.XMLDocument ? iframe.contentWindow.document.XMLDocument : io.contentWindow.document;
} else if (iframe.contentDocument) {
xml.responseText = io.contentDocument.document.body ? iframe.contentDocument.document.body.innerHTML : null;
xml.responseXML = io.contentDocument.document.XMLDocument ? iframe.contentDocument.document.XMLDocument : io.contentDocument.document;
}
success(xml.responseText, xml.responseXML);
} else {
}
} catch (e) {
if(error)
{
error(e);
}
}
div.parentNode.removeChild(div);
//newform.parentNode.removeChild(newform);
//iframe.parentNode.removeChild(iframe);
}
/*
//建立一個新的form,用於暫時儲存上傳檔案框,hidden控制項等
var newform = document.createElement('form');
newform.action = url;
newform.enctype = 'multipart/form-data';
newform.method = 'post';
newform.target = iframeName;
newform.style.display = 'none';
newform.id = "newform";
newform.name = 'newfrom';
//建立一個IFrame,用於做新的form的提交後顯示伺服器返回內容的視窗
var iframe = document.createElement('iframe');
iframe.name = iframeName;
//將newform和iframe添加到body
body.appendChild(iframe);
body.appendChild(newform);
*/
var str = '<form action="' + url + '" enctype="multipart/form-data" method="post" target="' + iframeName + '"></form><iframe name="' + iframeName + '"></iframe>';
var div = document.createElement('div');
div.innerHTML = str;
div.style.display = "none";
newform = div.getElementsByTagName('form')[0];
iframe = div.getElementsByTagName('iframe')[0];
body.appendChild(div);
iframe.onload = uploadedCallback;
if (iframe.attachEvent) {
iframe.attachEvent('onload', uploadedCallback);
}
for (var i = 0; i < files.length; i++) {
var newfile = files[i].cloneNode(true);
fileArr.push({ tmpfile: newfile, orifile: files[i] });
files[i].parentNode.insertBefore(newfile, files[i]);
newform.appendChild(files[i]);
}
//根據一些自訂的資料,建立input[type=file],添加到新的form
if (data) {
for (var attr in data) {
var hidden = document.createElement('input');
hidden.type = 'hidden';
hidden.name = attr;
hidden.value = data[attr];
newform.appendChild(hidden);
}
}
if (ajaxOptions.timeout) {
setTimeout(function() { uploadedCallback('timeout'); }, ajaxOptions.timeout);
}
//提交form
newform.submit();
//恢複原始檔案上傳框
for (i = 0; i < fileArr.length; i++) {
fileArr[i].tmpfile.parentNode.insertBefore(fileArr[i].orifile, fileArr[i].tmpfile);
fileArr[i].tmpfile.parentNode.removeChild(fileArr[i].tmpfile, true);
}
//newform.parentNode.removeChild(newform);
//iframe.parentNode.removeChild(iframe);
}