AjaxFileUpload + *.ashx 文件上传在IE8.0(XP,VS2010,Development Server)下的注意
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 ; } } } } |
标签:
Ajax文件上传
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
2011-02-25 Unity2.0与interception的配合使用(EntLib5.0)
2008-02-25 SiteFactory技术实现QQ群19452522用户交流贴