Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpResponseMessage返回ByteArrayContent结果-在chrome中预览文档_C#_.net_Asp.net Web Api_Httpresponsemessage - Fatal编程技术网

C# HttpResponseMessage返回ByteArrayContent结果-在chrome中预览文档

C# HttpResponseMessage返回ByteArrayContent结果-在chrome中预览文档,c#,.net,asp.net-web-api,httpresponsemessage,C#,.net,Asp.net Web Api,Httpresponsemessage,我试图在Chrome中预览一个文件,但它一直在下载 [HttpGet] [ResponseType(typeof(ByteArrayContent))] public HttpResponseMessage Download([FromUri] int uploadId) { try { Upload upload = UploadController.LoadByPrimaryKey(uploadId);

我试图在Chrome中预览一个文件,但它一直在下载

    [HttpGet]
    [ResponseType(typeof(ByteArrayContent))]
    public HttpResponseMessage Download([FromUri] int uploadId)
    {
        try
        {
            Upload upload = UploadController.LoadByPrimaryKey(uploadId);

            var path = upload.FullPath + "\\" + upload.UploadGuid + upload.Extension;
            var mimeType = MimeTypeMap.GetMimeType(upload.Extension);

            MemoryStream pdf = new MemoryStream(File.ReadAllBytes(path));
            HttpResponseMessage result = null;
            result = Request.CreateResponse(HttpStatusCode.OK);
            result.Content = new ByteArrayContent(pdf.ToArray());
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline");
            result.Content.Headers.ContentDisposition.FileName = upload.OriginalFileName;
            result.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType);

            return result;
        }
        catch (Exception ex)
        {
          //..
        }
    }
这是菲德勒留下的痕迹

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 793708
Content-Type: application/pdf
Expires: -1
Server: Microsoft-IIS/10.0
Access-Control-Allow-Origin: http://localhost:6701
Access-Control-Allow-Credentials: true
Content-Disposition: inline; filename="1451048-Customer Acceptance.pdf"
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sun, 19 Jun 2016 10:52:35 GMT
...
我看到了,但我仍然有困难


任何帮助都将不胜感激。

我看不出您的代码有任何问题,但您的浏览器设置似乎更有问题(尽管大多数浏览器都有默认设置来呈现PDF格式,但您已经看到其他设置)

如果在Mozilla中尝试

  • 单击菜单按钮并选择选项
  • 选择应用程序面板
  • 在列表中找到可移植文档格式(PDF),然后单击它以选择它
  • 单击“操作”列中用于上述条目的下拉箭头,然后选择“在firefox中预览” 参考-

  • 转到“选项”(单击右上角的扳手按钮)
  • 转到:引擎盖下->内容设置->插件
  • 单击“管理单个插件…”
  • 找到Adobe Acrobat插件(或任何其他PDF查看器)并确保其已启用
  • 我的API中有完全相同的代码,它允许我基于ContentDispositionHeaderValue呈现PDF或下载(内联呈现和附件下载)

    请参阅下面我的服务器的响应标题

    HTTP/1.1 200 OK
    Cache-Control: private
    Content-Length: 46270
    Content-Type: application/pdf
    Server: Microsoft-IIS/10.0
    Content-Disposition: inline; filename=Sample.pdf
    X-AspNet-Version: 4.0.30319
    X-Powered-By: ASP.NET
    Date: Mon, 20 Jun 2016 03:38:39 GMT
    
    希望这有帮助