No refreshing upload of original ajax files

Source: Internet
Author: User
Function submitFun (){
Var files = [document. getElementById ('scs')];
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;
// Return value of the server
Var xml = {};
Var url = ajaxOptions. url;
Var files = ajaxOptions. files;
Var data = ajaxOptions. data;
Var success = ajaxOptions. success;
Var error = ajaxOptions. error;

// An object array. The object has two attributes. One represents the temporary input File Upload box and the other represents the original file upload box.
Var fileArr = [];

// Callback function after submission or timeout
Var uploadedCallback = function (timeout ){
Try {
// If this callback function is called not because of timeout
If ('timeout '! = Timeout ){
If (iframe. contentWindow ){
Xml. responseText = iframe.content+doc ument. body? Iframe.contentdomaindoc ument. body. innerHTML: null;
Xml. responseXML = iframe.content+doc ument. XMLDocument? Iframe.content+doc ument. XMLDocument: io.content+doc ument;

} Else if (iframe. contentDocument ){
Xml. responseText = io.contentDocument.doc ument. body? Iframe.contentDocument.doc ument. body. innerHTML: null;
Xml. responseXML = io.contentDocument.doc ument. XMLDocument? Iframe.contentDocument.doc ument. XMLDocument: io.contentDocument.doc ument;
}
Success (xml. responseText, xml. responseXML );
} Else {

}
} Catch (e ){
If (error)
{
Error (e );
}
}
Div. parentNode. removeChild (div );
// Newform. parentNode. removeChild (newform );
// Iframe. parentNode. removeChild (iframe );
}

/*
// Create a new form for temporarily saving the uploaded file box and the hidden control.
Var newform = document. createElement ('form ');
Newform. action = url;
Newform. enctype = 'multipart/form-data ';
Newform. method = 'post ';
Newform.tar get = iframeName;
Newform. style. display = 'none ';
Newform. id = "newform ";
Newform. name = 'newfrom ';


// Create an IFrame for displaying the content returned by the server after the new form is submitted
Var iframe = document. createElement ('iframe ');
Iframe. name = iframeName;
// Add newform and iframe to the 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]);
}
// Create input [type = file] based on custom data and add it to the new form
If (data ){
For (var attr in data ){
Var hidden = document. createElement ('input ');
Hidden. type = 'den den ';
Hidden. name = attr;
Hidden. value = data [attr];
Newform. appendChild (hidden );
}
}
If (ajaxOptions. timeout ){
SetTimeout (function () {uploadedCallback ('timeout') ;}, ajaxOptions. timeout );
}
// Submit the form
Newform. submit ();
// Restore the original file upload box
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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.