Ajaxfileupload Source
Solve the problem of uploading success and not going success
Troubleshoot high-version jquery compatibility issues
Jquery.extend ({
Createuploadiframe:function (ID, URI)
{
Create frame
var Frameid = ' juploadframe ' + ID;
var iframehtml = ' <iframe id= ' + Frameid + ' "name=" ' + Frameid + ' "style=" position:absolute; top:-9999px; Left:-9999px "';
if (window. ActiveXObject)
{
if (typeof uri== ' Boolean ') {
iframehtml + = ' src= ' + ' javascript:false ' + ';
}
else if (typeof uri== ' string ') {
iframehtml + = ' src= ' + uri + ' ";
}
}
iframehtml + = '/> ';
JQuery (iframehtml). AppendTo (Document.body);
return JQuery (' # ' + Frameid). Get (0);
},
Createuploadform:function (ID, fileelementid, data)
{
Create form
var formId = ' juploadform ' + ID;
var fileId = ' juploadfile ' + ID;
var form = jQuery (' <form action= "" method= "POST" name= "' + formId + '" id= "' + formId + '" enctype= "multipart/form-data "></form>");
if (data)
{
for (var i in data)
{
JQuery (' <input type= ' hidden "name=" ' + i + ' "value=" ' + data[i] + '/> '). AppendTo (form);
}
}
var oldelement = jQuery (' # ' + Fileelementid);
var newelement = Oldelement.clone (true);
Newelement[0].files = Oldelement[0].files;
OLDELEMENT.ATTR (' id ', fileId);
Oldelement.before (newelement);
Oldelement.appendto (form);
Set attributes
JQuery (Form). CSS (' position ', ' absolute ');
JQuery (Form). CSS (' top ', ' -1200px ');
JQuery (Form). CSS (' left ', ' -1200px ');
JQuery (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, (typeof (S.data) = = ' undefined '? false:s.data));
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.contentwindow.document.body?io.contentwindow.document.body.innerhtml:null;
Xml.responsexml = io.contentwindow.document.xmldocument?io.contentwindow.document.xmldocument: Io.contentWindow.document;
}else if (io.contentdocument)
{
Xml.responsetext = Io.contentdocument.document.body?io.contentdocument.document.body.innerhtml:null;
Xml.responsexml = io.contentdocument.document.xmldocument?io.contentdocument.document.xmldocument: Io.contentDocument.document;
}
}catch (e)
{
Jquery.handleerror (s, XML, NULL, E);
}
if (XML | | istimeout = = "Timeout")
{
Requestdone = true;
var status;
try {
Status = istimeout! = "Timeout"? "Success": "Error";
Make sure, 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 is 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
{
JQuery (IO). Remove ();
JQuery (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 form = jQuery (' # ' + formId);
JQuery (Form). attr (' action ', S.url);
JQuery (Form). attr (' method ', ' POST ');
JQuery (Form). attr (' target ', Frameid);
if (form.encoding)
{
JQuery (Form). attr (' Encoding ', ' multipart/form-data ');
}
Else
{
JQuery (Form). attr (' enctype ', ' multipart/form-data ');
}
JQuery (Form). Submit ();
} catch (E)
{
Jquery.handleerror (s, XML, NULL, E);
}
JQuery (' # ' + Frameid). Load (uploadcallback);
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")
data = Data.replace ("<pre>", ""). Replace ("</pre>", "");
Evaluate scripts within HTML
if (type = = "html")
JQuery ("<div>"). HTML (data). Evalscripts ();
return data;
}
})
HTML code and service-side code included
<input type= "File" id= "ImportFile" name= "ImportFile" accept= "Text/plain" style= "Display:none"/>
#region Upload a file to read the imported content
Public Jsonresult Upload ()
{
Httpfilecollectionbase HFC = request.files;
String Path = "";
list<lgtdandlttd> list = new list<lgtdandlttd> ();
String physicalpath = "";
if (HFC. Count > 0)
{
if (Hfc[0]. Filename.contains ("\ \"))
{
int index = hfc[0]. Filename.lastindexof (' \ \ ');
Path = "/upload" +hfc[0]. Filename.substring (index, hfc[0]. Filename.length-index);
}
Else
Path = "/upload/" + System.DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss:ffff") + System.IO.Path.GetFileName (hfc[0]. FileName);
PhysicalPath = Server.MapPath (Path);
Hfc[0]. SaveAs (PhysicalPath);
StreamReader sr = new StreamReader (physicalpath);
LGTDANDLTTD sta = new Lgtdandlttd ();
while (Sr. Peek () >=0)
{
Sta. LGTD = Sr. ReadLine (). Split (' | ') [0];
Sta. Lttd = Sr. ReadLine (). Split (' | ') [1];
List. ADD (STA);
}
}
return Json (list);
}
#endregion
Ajaxfileupload.js for jquery1.11.1