javascript - Angularjs 在新的弹出窗口中显示 pdf/图像文件

标签 javascript angularjs asp.net-web-api

我正在尝试在新的弹出窗口中显示图像和 pdf 文件。该文档以字节数组的形式保存在数据库中。这是我的服务器端代码。

 public HttpResponseMessage GetUserDocumentByDocumentId(int documentId)
    {
        HttpResponseMessage result = null;
        try
        {

            RegistrationManager registrationManager = new RegistrationManager();
            var file = registrationManager.GetUserDocumentByDocumentId(documentId);
            result = Request.CreateResponse(HttpStatusCode.OK);
            result.Content = result.Content = new ByteArrayContent(file.DocumentObject);
            result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
            result.Content.Headers.ContentType = new MediaTypeHeaderValue(file.DocumentExtension);
            result.Content.Headers.ContentDisposition.FileName = file.DocumentName;
            result.Content.Headers.Add("x-filename", file.DocumentName);
            return result;
        }
        catch (Exception ex)
        {
            throw new HttpResponseException(FBSHttpResponseException.HttpResponseMessage(ex));
        }
    }

客户端代码是这样的

 var windowWidth = 1000;
 var windowHeight = 550;
 var left = (screen.width / 2) - (windowWidth / 2);
 var top = ((screen.height / 2) - (windowHeight / 2)) - 50;
 if (typeof reportWindow != 'undefined' && reportWindow.closed == false) {
 reportWindow.close();
 }
 var url = baseUrl + "/api/user/getuserdocumentbydocumentid/" + userDocId;
 $window.open(url, 'PopupReport', 'scrollbars=yes,status=yes,toolbar=yes,menubar=no,location=no,resizable=no,fullscreen=yes, width=' + windowWidth + ', height=' + windowHeight + ', top=' + top + ', left=' + left);

但它不是在新窗口中显示它们,而是打开窗口并下载 pdf 或图像文件。

最佳答案

您应该在后端将 Content-Disposition header 设置为“内联”,以便浏览器尝试呈现文件而不是下载它:

result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("inline");

查看这个问题Content-Disposition:What are the differences between "inline" and "attachment"?

关于javascript - Angularjs 在新的弹出窗口中显示 pdf/图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37048124/

相关文章:

model-binding - WebApi2 : Custom parameter binding to bind partial parameters

javascript - 谷歌图表图例位置不起作用

javascript - 从弹出窗口发送消息到内容脚本的 chrome 扩展

angularjs - Angular-js 代码缩小错误 : Unexpected token: operator (>)

javascript - 如何在 Angular ng-repeat 中按类别键过滤数组中的对象

asp.net-web-api - 针对每个 Web API 请求设置 JSON CamelCase

javascript - 调整选择框的大小以容纳所选文本

Javascript reduce() 直到值之和 < 变量

javascript - 如何捕获指令中的复选框事件?

asp.net-mvc - MVC Web Api - 准系统最小项目结构