$(function () { $(".uploadfile").upload({ action: 'CourseXMLFileUploadHander.ashx', name: 'xml', params: { 'type': 'uploadCourseXMLFile', 'rand': Math.random() }, onSelect: function (self, element) { this.autoSubmit = false; var re = new RegExp("(xml){1}quot;, "i"); if (!re.test(this.filename())) { alert("Only xml file can be uploaded"); } else { this.submit(); } }, onSubmit: function (self, element) { $('.uploadfile').hide(); $('#ajax_update').parent().show(); //alert('Uploading file...'); }, onComplete: function (data, self, element) { $('#ajax_update').parent().hide(); $('.uploadfile').show(); self.resetInput(); try { var ret = data; if (ret.indexOf("exception") >= 0) { alert('Upload file exception: ' + eval(data)[0].exception); } else { showSuccess('File is successfully Load.'); uploadSuccess(ret); } } catch (err) { alert(data); } } }); });
==============================================
<asp:Button ID="btnUploadXMLFile" runat="server" Text="LoadXMLFile" CausesValidation="false" CssClass="uploadfile" />
<script src="../js/jquery.ocupload/jquery.ocupload-1.1.2-mod.js" type="text/javascript"></script>
================================================
public class CourseXMLFileUploadHander : IHttpHandler { public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) { try { HttpPostedFile file = context.Request.Files["xml"]; //string folder = context.Server.MapPath("../UploadedFiles"); string fileName = Path.GetFileName(file.FileName); if (Path.GetExtension(fileName).ToLower() != ".xml") { context.Response.Write(string.Concat(@"[{""exception"":""The uploaded file is not a xml file.""}]")); return; } string content = string.Empty; using (StreamReader sr = new StreamReader(file.InputStream, System.Text.Encoding.Default)) { content = sr.ReadToEnd(); } context.Response.Write(ConvertStr(content)); } catch (Exception ex) { StringBuilder sb = new StringBuilder(); while (ex != null) { sb.AppendFormat("\\r\\n. {0}", ex.Message); ex = ex.InnerException; } context.Response.Write(string.Concat(@"[{""exception"":""", sb.ToString(), @"""}]")); } } private static string ConvertStr(string inputString) { string retVal = inputString; retVal = retVal.Replace("&", "&"); retVal = retVal.Replace("\"", """); retVal = retVal.Replace("<", "<"); retVal = retVal.Replace(">", ">"); //retVal = retVal.Replace(" ", " "); //retVal = retVal.Replace(" ", " "); //retVal = retVal.Replace("\t", " "); //retVal = retVal.Replace("\r", "<br>"); return retVal; } }