No refreshing upload code for jquery instance files
No refreshing upload code for jquery instance files
JQuery. extend ({
CreateUploadIframe: function (id, uri)
{
// Create frame
Var frameId = 'juploadframe '+ id;
If (window. ActiveXObject ){
Var io = document. createElement ('<iframe id = "' + frameId + '" name = "' + frameId + '"/> ');
If (typeof uri = 'boolean '){
Io. src = 'javascript: false ';
}
Else if (typeof uri = 'string '){
Io. src = uri;
}
}
Else {
Var io = document. createElement ('iframe ');
Io. id = frameId;
Io. name = frameId;
}
Io. style. position = 'absolute ';
Io. style. top = '-1000px ';
Io. style. left = '-1000px ';
Document. body. appendChild (io );
Return io
},
CreateUploadForm: function (id, fileElementId)
{
// Create form
Var formId = 'juploadform' + id;
Var fileId = 'juploadfile' + id;
Var form = $ ('<form action = "" method = "POST" name = "' + formId + '" id = "' + formId + '" enctype = "multipart/ form-data "> </form> ');
Var oldElement = $ ('#' + fileElementId );
Var newElement = $ (oldElement). clone ();
$ (OldElement). attr ('id', fileId );
$ (OldElement). before (newElement );
$ (OldElement). appendTo (form );
// Set attributes
Parameters (form).css ('position', 'absolute ');
((Form).css ('top', '-margin px ');
Optional (form).css ('left', '-margin px ');
$ (Form). appendTo ('body ');
Return form;
},
AjaxFileUpload: function (s ){
// TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
S = jQuery. extend ({}, jQuery. ajaxSettings, s );
Var id = new Date (). getTime ()
Var form = jQuery. createUploadForm (id, s. fileElementId );
Var io = jQuery. createUploadIframe (id, s. secureuri );
Var frameId = 'juploadframe '+ id;
Var formId = 'juploadform' + id;
// Watch for a new set of requests
If (s. global &&! JQuery. active ++)
{
JQuery. event. trigger ("ajaxStart ");
}
Var requestDone = false;
// Create the request object
Var xml = {}
If (s. global)
JQuery. event. trigger ("ajaxSend", [xml, s]);
// Wait for a response to come back
Var uploadCallback = function (isTimeout)
{
Var io = document. getElementById (frameId );
Try
{
If (io. contentWindow)
{
Xml. responseText = io.contentdomaindoc ument. body? Io.contentdomaindoc ument. body. innerHTML: null;
Xml. responseXML = io.content20.doc ument. XMLDocument? Io.content+doc ument. XMLDocument: io.content+doc ument;
} Else if (io. contentDocument)
{
Xml. responseText = io.contentDocument.doc ument. body? Io.contentDocument.doc ument. body. innerHTML: null;
Xml. responseXML = io.contentDocument.doc ument. XMLDocument? Io.contentDocument.doc ument. XMLDocument: io.contentDocument.doc ument;
}
} Catch (e)
{
JQuery. handleError (s, xml, null, e );
}
If (xml | isTimeout = "timeout ")
{
RequestDone = true;
Var status;
Try {
Status = isTimeout! = "Timeout "? "Success": "error ";
// Make sure that the request was successful or notmodified
If (status! = "Error ")
{
// Process the data (runs the xml through httpData regardless of callback)
Var data = jQuery. uploadHttpData (xml, s. dataType );
// If a local callback was specified, fire it and pass it the data
If (s. success)
S. success (data, status );
// Fire the global callback
If (s. global)
JQuery. event. trigger ("ajaxSuccess", [xml, s]);
} Else
JQuery. handleError (s, xml, status );
} Catch (e)
{
Status = "error ";
JQuery. handleError (s, xml, status, e );
}
// The request was completed
If (s. global)
JQuery. event. trigger ("ajaxComplete", [xml, s]);
// Handle the global AJAX counter
If (s. global &&! -- JQuery. active)
JQuery. event. trigger ("ajaxStop ");
// Process result
If (s. complete)
S. complete (xml, status );
JQuery (io). unbind ()
SetTimeout (function ()
{Try
{
$ (Io). remove ();
$ (Form). remove ();
} Catch (e)
{
JQuery. handleError (s, xml, null, e );
}
}, 100)
Xml = null
}
}
// Timeout checker
If (s. timeout> 0)
{
SetTimeout (function (){
// Check to see if the request is still happening
If (! RequestDone) uploadCallback ("timeout ");
}, S. timeout );
}
Try
{
// Var io = $ ('#' + frameId );
Var form = $ ('#' + formId );
$ (Form). attr ('action', s. url );
$ (Form). attr ('method', 'post ');
$ (Form). attr ('target', frameId );
If (form. encoding)
{
Form. encoding = 'multipart/form-data ';
}
Else
{
Form. enctype = 'multipart/form-data ';
}
$ (Form). submit ();
} Catch (e)
{
JQuery. handleError (s, xml, null, e );
}
If (window. attachEvent ){
Document. getElementById (frameId). attachEvent ('onload', uploadCallback );
}
Else {
Document. getElementById (frameId). addEventListener ('load', uploadCallback, false );
}
Return {abort: function (){}};
},
UploadHttpData: function (r, type ){
Var data =! Type;
Data = type = "xml" | data? R. responseXML: r. responseText;
// If the type is "script", eval it in global context
If (type = "script ")
JQuery. globalEval (data );
// Get the JavaScript object, if JSON is used.
If (type = "json ")
Eval ("data =" + data );
// Evaluate scripts within html
If (type = "html ")
JQuery ("<div>" ).html (data). evalScripts ();
// Alert ($ ('param', data). each (function () {alert ($ (this). attr ('value '));}));
Return data;
}
})