Ajaxfileuploader Upload plugin compatibility good Ie Chrome Firefox
HTML:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Service side:
Using System.Web;
Using System.IO; namespace Ajaxfileuploader.ajax {///<summary>///ajaxfileuploader Summary description///</summary> Publ
IC class Ajaxfileuploader:ihttphandler {public void ProcessRequest (HttpContext context) { if (context. Request.Files.Count > 0) {string path = context.
Server.MapPath ("~/temp"); if (!
Directory.Exists (path)) directory.createdirectory (path); var file = context.
Request.files[0];
String FileName; if (HttpContext.Current.Request.Browser.Browser.ToUpper () = = "IE") {string[] files = File.
Filename.split (new char[] {' \ \} '); FileName = Files[files.
LENGTH-1]; else {fileName = file.
FileName;
} String msg = ""; String strFileName = fileName; if (string.
IsNullOrEmpty (strFileName)) {msg = "{"; MSG + String. Format ("error: ' {0} ', \ n", "Select File.")
"); MSG + String. Format ("msg: ' {0} ' \ n", string.)
Empty);
msg + = "}";
else {fileName = Path.Combine (Path, fileName); File.
SaveAs (FileName);
msg = "{"; MSG + String. Format (' ERROR: ' {0} ', \ n ', string.
Empty); MSG + String.
Format ("msg: ' {0} ' \ n", strFileName);
msg + = "}"; } context.
Response.Write (msg);
} public bool IsReusable {get {true;
}
}
}
}
Plug - ins:
Ajaxfileupload.js 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 frame var formid = ' Juploadform ' +
Id
var Fileid = ' juploadfile ' + ID; var form = jQuery (' <form action= "" method= "POST" name= "' + Formid + '" id= "' + Formid + '" enctype= "Multipart/form-dat A "></form> '); if (data) {for (var i in data) {jQuery (' <input type= "hidden" name= "' + i + '" value= ' +
Data + '/> '). Appendto (form);
} var oldelement = JQuery (' # ' + Fileelementid);
var newelement = jQuery (oldelement). Clone ();
JQuery (oldelement). attr (' id ', fileid);
JQuery (Oldelement). before (newelement);
JQuery (oldelement). appendto (form);
Set properties JQuery (form). CSS (' position ', ' absolute ');
JQuery (Form). CSS (' top ', ' -1200px ');
JQuery (Form). CSS (' left ', ' -1200px ');
JQuery (Form). Appendto (' body ');
return form;
}, Ajaxfileupload:function (s) {s = Jquery.extend ({}, jquery.ajaxsettings, s); var id = new Date (). GetTime () var form = Jquery.createuploadform (ID, S.fileelementid, (typeof (S.data) = ' Undefin Ed '?
False:s.data));
var io = jquery.createuploadiframe (ID, S.secureuri); var Frameid = ' JUPloadframe ' + ID;
var formid = ' juploadform ' + ID;
Monitor request if (S.global &&!jquery.active++) {JQuery.event.trigger ("Ajaxstart");
var requestdone = false;
Create Request object var XML = {} if (S.global) JQuery.event.trigger ("Ajaxsend", [XML, S]);
Wait for a response to return var uploadcallback = function (istimeout) {var io = document.getElementById (Frameid); try {if (Io.contentwindow) {xml.responsetext = Io.contentWindow.document.bo Dy?
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.contentdocum
Ent.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"; Request Success if (Status!= "error") {//processing data var = jqu
Ery.uploadhttpdata (XML, S.datatype);
if (s.success) s.success (data, status);
Triggers global return 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); //Request Complete if (S.global) JQuery.event.trigger ("Ajaxcomplete", [XML, S]
); Handle Global Ajax Counters if (S.global &&!--jquery.active) JQuery.event.trigger ("Ajaxst
Op ");
Processing results 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); }}, XML = null}}//timeout check if (S.timeout >
0) {settimeout (function () {if (!requestdone) uploadcallback ("timeout"); }, S.timeout);
The 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 (type = = "Script") jquery.globaleval (data);
if (type = = "json") eval ("data =" + data); if (type = = "html") JQuery ("<div> ").
HTML (data). Evalscripts ();
return data; }) more upload Plug-ins http://www.cnblogs.com/zengxiangzhan/category/269831.html
This article links