在ajax.action链接的部分页面上显示内联文档

| 我在页面上调用ajax.action链接。这将显示文档的名称。当我单击文档时,控制器会触发ajax请求,该请求将返回文件内容结果,我希望此文件在目标ID div下的浏览器中内联显示。 代码-字节流= fs.ToArray();                 fs.Close();                 Response.AppendHeader(\“ Content-Disposition \”,String.Format(\“ inline; filename = {0} \”,fileName));                 返回File(bytestream,\“ application / pdf \”); 问题是,文件显示为流,并且没有正确显示内容。
    <legend>Document</legend>
    <% if (Model.PresentDocument != null)
       { %>
    <li><%: Ajax.ActionLink(Model.PresentDocument, \"GetDocumentPage\", new RouteValueDictionary(new { controller = \"Document\", action = \"GetDocumentPage\", id = Model.PresDocId }), new AjaxOptions { HttpMethod = \"Post\", UpdateTargetId = \"Document\" })%></li>
    <%} %>
    <div id=\"Document\">
    </div>
我是否需要为此div进行特定操作才能显示内联pdf?     
已邀请:
您不能使用AJAX下载文件。实现此目的的一种可能方法是使用普通链接,然后单击此链接,然后使用javascript动态生成iframe,并将iframe源指向控制器操作:
<%= Html.ActionLink(
    Model.PresentDocument, 
    \"GetDocumentPage\", 
    \"Document\",
    new { id = Model.PresDocId }, 
    new { id = \"displayPdf\" }
) %>
您可以使用AJAXify这样(使用jQuery):
$(function() {
    $(\'#displayPdf\').click(function() {
        $(\'#Document\').html(
            $(\'<iframe/>\', {
                src: this.href,
                width: \'300px\',
                height: \'150px\'
            })
        );
        return false;
    });
});
假设您的控制器动作可通过GET请求访问:
public ActionResult GetDocumentPage(string id)
{
    byte[] pdf = ...
    Response.AppendHeader(\"Content-Disposition\", String.Format(\"inline; filename={0}\", fileName));
    return File(pdf, \"application/pdf\");
}
    

要回复问题请先登录注册