AjaxFileUpload + *.ashx 文件上传在IE8.0(XP,VS2010,Development Server)下的注意
2012-02-25 09:31
381 查看
ashx,中返回类型设置成 json,plain时,返回的Json代码解析都出错,调试发现实际返回的除了json字符串外还有,其他html或xml导致无法解析成json对象
服务器端
function UploadFile(elmFlag) { $.ajaxFileUpload({ url: '/FileUpload.ashx', secureuri: false, fileElementId: "fu" + elmFlag, dataType: "json", success: function (data, status) { if (data.Code == 200) { alert( unescape(data.Name)); } else { alert( unescape(data.Msg)); } $("#c_" + elmFlag).empty(); $("#c_" + elmFlag).append("<input type='file' name='fu" + elmFlag + "' id='fu" + elmFlag + "' onchange=\"UploadFile('" + elmFlag + "')\" />"); }, error: function (data, status, e) { alert("错误:上传组件错误,请检察网络!"); } }); }
服务器端
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Script.Serialization; using System.IO; using System.Web.Hosting; using F.Studio.Common; using Microsoft.Practices.Unity; using JLMFG.Doc.BLL; using JLMFG.Doc.Domain; namespace JLMFG.Doc.Web { /// <summary> /// FileUpload 的摘要说明 /// </summary> public class FileUpload : IHttpHandler,IBuildUp { static readonly string C_FileRoot = "/Files/"; [Dependency] public doc_DocManager DocMgr { get; set; } private HttpRequest Request { get; set; } private HttpResponse Response { get; set; } private JavaScriptSerializer Serializer { get; set; } public void ProcessRequest(HttpContext context) { Request = context.Request; Response = context.Response; Serializer = new JavaScriptSerializer(); Response.ContentType = "text/html"; Response.Expires = -1; try { if (Request.Files.Count <= 0) throw new Exception("没有文件上传!"); SaveFile(); } catch (Exception ex) { var resp=new UploadResponse(){Code=400,Msg=StringHelper.Escape(ex.Message)}; Response.Write(Serializer.Serialize(resp)); } Response.End(); } private void SaveFile() { var file = Request.Files[0]; var ext = Path.GetExtension(file.FileName); //确保目录存在 string path = C_FileRoot + DateTime.Now.ToString("yyyy-MM-dd") + "/"; if (!Directory.Exists( HostingEnvironment.MapPath(path))) { Directory.CreateDirectory(HostingEnvironment.MapPath(path)); } //合成文件名 var filename= path + Guid.NewGuid().ToString("N").Substring(0,8) + ext; var resp = new UploadResponse(); resp.MIME = file.ContentType; resp.Size = file.ContentLength / 1024; resp.Name =StringHelper.Escape( Path.GetFileNameWithoutExtension(file.FileName)); resp.Path =StringHelper.Escape( filename); resp.Code = 200; resp.Msg = "Success"; //保持文件 file.SaveAs(System.Web.Hosting.HostingEnvironment.MapPath(filename)); Response.Write(Serializer.Serialize(resp)); } public bool IsReusable { get { return false; } } public class UploadResponse { public int Code { get; set; } public string Msg { get; set; } public string Path { get; set; } public string Name { get; set; } public long Size { get; set; } public string MIME { get; set; } } } }
相关文章推荐
- AjaxFileUpload文件上传 及注意事项
- springMVC + easyui + $.ajaxFileUpload实现文件上传注意事项
- springMVC + easyui + $.ajaxFileUpload实现文件上传注意事项
- Jquery ajaxfileupload.js结合.ashx文件实现无刷新上传
- ajax+php无刷新文件上传(ajaxuploadfile)
- WebWork之--使用fileUpload interceptor实现文件上传[基本配置+注意事项]
- Ajaxfileupload 上传文件后返回response的contentType错误问题
- php使用AjaxFileUpload上传图片文件实例
- spring mvc ajaxfileupload文件上传返回json下载问题
- SpringMVC结合ajaxfileupload.js实现文件无刷新上传
- 使用ajaxfileupload上传文件
- easyui+ajaxfileupload,无刷新文件上传
- ajaxFileUpload 上传文件
- $ajaxFileUpload可以带参数文件上传源码及使用
- ajaxFileUpload plugin上传文件 chrome、Firefox中出现SyntaxError:unexpected token <
- 解决ajaxfileupload上传文件在IE浏览器返回data为空问题
- ajaxfileupload 实现多文件上传
- ajaxFileUpload+struts2实现多文件上传(动态添加文件上传框)
- SpringMVC结合ajaxfileupload.js实现ajax无刷新文件上传
- 文件上传JQuery插件 ajaxFileUpload 不严谨的bug