二开实现给文件下载接口设置 Content-Disposition 响应头

【应用场景】
部分加密软件部署在网关,需要根据响应头中的 Content-Dispositon: attachment; filename=****.xlsx 来判断文件类型,从而决定是否要加密。
【注意事项】
如无开发能力,可直接下载文末的附件使用,但不保证所有版本可用。
【实现步骤】
<1>新建类库项目,添加Kingdee.BOS.dll 和 Kingdee.BOS.Web.dll 的引用。

<2>安装 Lib.Harmony 的 NuGet 包。

<3>新建 Dowmload 类 和 DownloadExportFile 类,代码如下。
namespace Custom.Web
{
public class Download : Kingdee.BOS.Web.FileServer.Download
{
static Download()
{
Patcher.ApplyPatches();
}
}
}namespace Custom.Web
{
public class DownloadExportFile : Kingdee.BOS.Web.FileServer.DownloadExportFile
{
static DownloadExportFile()
{
Patcher.ApplyPatches();
}
}
}<4>新建 Patcher 类,代码如下。
using HarmonyLib;
using Kingdee.BOS.Log;
using System;
using System.Reflection;
using System.Web;
namespace Custom.Web
{
internal class Patcher
{
private static bool _patched = false;
private static object _patchedLock = new object();
public static void ApplyPatches()
{
try
{
if (!_patched)
{
lock (_patchedLock)
{
if (!_patched)
{
var harmony = new Harmony("filedownload.startup.patch");
// 获取目标类型的 MethodBase
MethodBase targetMethod = Type.GetType("Kingdee.BOS.KDHttpUtility.RequestUtility,Kingdee.BOS")
.GetMethod("AddAttachmentHeader", new Type[] { typeof(HttpContext), typeof(string) });
// 定义补丁方法
var prefixMethod = typeof(Patcher).GetMethod("PrefixMethod", BindingFlags.Static | BindingFlags.NonPublic);
//var postfixMethod = typeof(Patcher).GetMethod("PostfixMethod", BindingFlags.Static | BindingFlags.NonPublic);
harmony.Patch(targetMethod, new HarmonyMethod(prefixMethod), null);
Logger.Error("Custom.Web.Patcher", "运行时补丁成功", null);
_patched = true;
}
}
}
}
catch (Exception ex)
{
Logger.Error("Custom.Web.Patcher", "运行时补丁失败", ex);
}
}
private static bool PrefixMethod(ref HttpContext context, ref stri二开实现给文件下载接口设置 Content-Disposition 响应头
【应用场景】部分加密软件部署在网关,需要根据响应头中的 Content-Dispositon: attachment; filename=****.xlsx 来判断文件类型,从而...
点击下载文档文档为doc格式
声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
上一篇
已经是第一篇



