C# · 12月 19, 2021

文件下载功能

代码如下:

/// <summary>/// 附件下载</summary><param name=”attachId”>附件ID</param>public void DownloadAttach(int attachId){ Edu_Attach edu_Attach = edu_AttachService.findById<Edu_Attach>(attachId); if (edu_Attach != null) { HttpContext context = (HttpContext)ctx.web.Context; if (context != ) { string filePathName = PathHelper.Map(sys.Path.DiskPhoto) + edu_Attach.AttachContent.Replace(“/static/upload/image”,””).Replace(/\\”); int pos = filePathName.LastIndexOf(string fileName = filePathName.Substring(pos + 1string UserAgent = context.Request.ServerVariables[http_user_agent].ToLower(); if (UserAgent.IndexOf(firefox”) == -) { //非火狐浏览器 context.response.addheader(content-dispositionattachment;filename=” + HttpUtility.UrlEncode(edu_Attach.AttachName)); } else { context.response.addheader( edu_Attach.AttachName); } FileStream fs = new FileStream(filePathName,FileMode.Open,FileAccess.Read); byte[] bArr = new byte[fs.Length]; fs.Read(bArr,0,bArr.Length); fs.Close(); context.Response.ContentEncoding = Encoding.UTF8; context.Response.BinaryWrite(bArr); context.Response.Flush(); context.Response.End(); } }}View Code