C# 使用MVC4写入响应流
我正在使用响应流将Word文档保存到我的MVC页面(使用Aspose.Words),但是在返回视图时,我收到了一个javascript警报“200OK”,但没有其他更改 我做得对吗C# 使用MVC4写入响应流,c#,asp.net-mvc,aspose,C#,Asp.net Mvc,Aspose,我正在使用响应流将Word文档保存到我的MVC页面(使用Aspose.Words),但是在返回视图时,我收到了一个javascript警报“200OK”,但没有其他更改 我做得对吗 wordDoc.Save(System.Web.HttpContext.Current.Response, "whatever", ContentDisposition.Inline, options); HttpContext.ApplicationInstance.CompleteRequest();
wordDoc.Save(System.Web.HttpContext.Current.Response, "whatever", ContentDisposition.Inline, options);
HttpContext.ApplicationInstance.CompleteRequest();
return View();
保存方法是有文档记录的。最好实现您自己的ActionResult,它将接受
wordDoc
并将其呈现给响应。在MVC中,这样做更自然
您的操作结果可能如下所示:
public class DocumentResult : ActionResult
{
private readonly Document document;
private readonly SaveOptions options;
public DocumentResult(Document document, SaveOptions options)
{
this.document = document;
this.options = options;
}
public override void ExecuteResult(ControllerContext context)
{
this.document.Save(System.Web.HttpContext.Current.Response, "whatever", ContentDisposition.Inline, this.options);
}
}
然后你可以在行动中使用它:
return new DocumentResult(wordDoc, options);
Save方法只接受一个HttpResponse,而不是一个HttpResponseBase-也许这就是我出错的地方。。。System.Web.HttpContext.Current.Response可以接受,但控制器的上下文可能没有使用它?HttpResponseBase是可模拟的HttpResponse,并且有一个类HttpResponseWrapper将HttpResponse包装到HttpResponseBase中(MVC会这样做),因此您可以安全地使用HttpContext.Current.Response。