我的视图中有以下AjaxActionLink代码:
@Ajax.ActionLink("Download", "DownloadDocument", "Document", new { Model.DocumentNumber, Model.DocumentName, TypeVisualization = TypeVisualization.Attachment }, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "modal-container", LoadingElementId = "ajaxLoading" }, new { @class = "modal-link" })
然后,在我的DocumentController中:
[HttpPost]
public async Task<ActionResult> DownloadDocument(DownloadFileRequest request)
{
DownloadFileCommand command = new DownloadFileCommand();
Mapper.Map(UserModel, command);
Mapper.Map(request, command);
var result = await documentRepository.DownloadDocument(command);
ContentDisposition cd = new ContentDisposition
{
FileName = Server.UrlPathEncode(result.DocumentName),
Inline = request.TypeVisualization == TypeVisualization.Inline
};
return new FileContentResultWithContentDisposition(result.Content, result.MimeType, cd);
}
该方法已被命中,但在返回后,没有文件被发送到浏览器并且抛出异常:
System.Web.HttpException (0x80004005): A public action method 'DownloadDocument' was not found on controller Document
en System.Web.Mvc.Controller.HandleUnknownAction(String actionName)
en System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState)
en System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
en System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
en System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
en System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller)
en System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
en System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
en System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
en System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
en System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
en System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
en System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
en System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
en System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
en System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
en System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
这里发生了什么?
答案 0 :(得分:0)
我建议您不要使用AJAX下载文件。即使调用成功,您在成功回调中也无法使用二进制内容,也无法将其保存到客户端计算机。例如,在您的AjaxLink中,您似乎已经指定了UpdateTargetId = "modal-container"
参数,但我想知道如果您的控制器操作返回二进制流,您希望在此div中发生什么?
相反,您可以使用HTML表单:
@using (Html.BeginForm("DownloadDocument", "Document", FormMethod.Post))
{
@Html.HiddenFor(x => x.DocumentNumber)
@Html.HiddenFor(x => x.DocumentName)
@Html.Hidden("TypeVisualization", TypeVisualization.Attachment)
<input type="submit" class="modal-link">Download</input>
}
就你的错误而言,我怀疑这与行动是async
的事实有关。这应该是开箱即用的。确保将操作正确放置在DocumentController
:
public class DocumentController: Controller
{
...
}