我正在使用此http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/
从WebAPI实施动态文件下载这里的关键是它在自定义属性过滤器中设置一个带有请求的cookie,该过滤器告诉浏览器文件何时完成生成,然后删除加载消息并将文件流式传输到浏览器。
当我在本地测试时,这一切都正常,但是当它部署到服务器时,我得到一个System.NullReferenceException。错误指向属性过滤器 - 上面的链接有一个如何在ASP.NET MVC 3中执行此操作的示例,但我使用4转换为此 -
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class FileDownloadAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
public string CookieName { get; set; }
public string CookiePath { get; set; }
public FileDownloadAttribute(string cookieName = "fileDownload", string cookiePath = "/")
{
CookieName = cookieName;
CookiePath = cookiePath;
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
CheckAndHandleFileResult(actionExecutedContext);
base.OnActionExecuted(actionExecutedContext);
}
/// <summary>
/// Write a cookie to inform jquery.fileDownload that a successful file download has occured
/// </summary>
/// <param name="filterContext"></param>
private void CheckAndHandleFileResult(HttpActionExecutedContext filterContext)
{
List<CookieHeaderValue> cookies = new List<CookieHeaderValue>();
CookieHeaderValue cookie = new CookieHeaderValue(CookieName, "true"){ Path = CookiePath };
cookies.Add(cookie);
filterContext.Response.Headers.AddCookies(cookies);
}
}
这是我得到的错误
System.Web.Http.Filters.ActionFilterAttribute.CallOnActionExecuted中的c:\ Dev \ Pensions \ Main \ API \ Attributes \ FileDownloadAttribute.cs:第28行中的API.Attributes.FileDownloadAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)中的System.NullReferenceException (HttpActionContext actionContext,HttpResponseMessage响应,异常异常)在System.Web.Http.Filters.ActionFilterAttribute。&lt;&gt; c_ DisplayClass2.b _1(CatchInfo 1 info) at System.Threading.Tasks.TaskHelpersExtensions.<>c__DisplayClass4
1.b__3()at System.Threading.Tasks.TaskHelpersExtensions.CatchImpl [TResult](任务任务,Func`1继续,CancellationToken cancellationToken)
有人有什么想法吗?我不明白为什么它在本地运行(通过IIS),但在部署到服务器时却没有。
编辑 -
错误中的第28行是指这一个
CheckAndHandleFileResult(actionExecutedContext);
编辑2-更新了完整的属性过滤器类