原創ajax檔案無重新整理上傳

來源:互聯網
上載者:User
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);
        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.