当我使用Controller.File方法时,操作返回空 [英] Action returns empty when I'm using Controller.File method

查看:193
本文介绍了当我使用Controller.File方法时,操作返回空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器中保存有一个PDF文件.现在,我试图以Content-Disposition = inline的形式返回此文件.

I have a PDF file save in my server. Now I'm trying to return this file as a Content-Disposition=inline.

所以我写了这个:

public FileResult Test()
{
    var ms = new MemoryStream();

    using (var fs = new FileStream(@"C:\Users\Me\Desktop\test.pdf", FileMode.Open))
    {
        fs.CopyTo(ms);
    }

    ms.Position = 0;

    Response.AppendHeader("Content-Disposition", "inline; filename=test.pdf");

    return File(ms, "application/pdf");
}

但是在我的浏览器(Chrome)中,我得到了一个空的pdf. 我尝试了其他浏览器并获得了此信息:

But in my browser (Chrome) I get a empty pdf. I tried other browsers and get this:

  • I.E. 8(或更少)-不起作用.
  • I.E. 9-有效.
  • Chrome(46.0.2490.71)-不起作用.
  • FireFox(32.0)-有效.

我的应用程序中还有另一个页面,该页面使用几乎相同的代码,并且适用于所有浏览器.

And I have another page in my application that is using pretty much the same code and works for all browsers.

任何人都可以帮助我理解为什么在某些浏览器中却出现这种奇怪行为而在其他浏览器中却没有这种行为吗?

Anyone can help me to understand why this strange behavior in some browsers and others not?

更新
我试图用此代码更改PDF(例如相同的代码,但更改为另一个pdf),并且适用于所有浏览器.

UPDATE
I tried to change my PDF in this code (like the same code but another pdf) and works for all browsers.

那么,PDF的内容是否可能导致这种情况?我尝试过,两个PDF在Adobe Reader和Win 8 Default PDF Reader中都可以正常打开.

So is it possible that the content of the PDF is causing that? I tried and both pdfs are open fine in Adobe Reader and Win 8 Default PDF Reader.

根据@Odrai的要求,我也尝试过:

As @Odrai request I tried too:

Response.AppendHeader("content-disposition", "attachment; filename=test");

但是也无法正常工作,因此无法更改接收文件的方式(带有附件的浏览器将进行下载,以打开文件).我需要以内联模式打开.

But didn't work too and change the way that file is received (with attachment the browser make a download instead to open the file). I need to open in inline mode.

作为@dustmouse的请求,我尝试直接在浏览器中打开pdf文件,并且出现相同的问题.

As @dustmouse request I tried to open the pdfs directly in the browsers and the same problem occurs.

所以现在我知道这不是程序问题,我打算结束这个问题.但是在此之前,我还有其他地方可以提出这个问题(例如与其他与stackoverflow相关的论坛,例如代码审查等)?

So now I understand it's not a program problem, I intend to close this question. But before that is there other place that I can put this question (like other forum related with stackoverflow, i.e. code review or something)?

推荐答案

此处所述, @AaronSieb :

这似乎是最新版本的Chrome中的错误.据报道,该文件已于2015年10月14日发布,并且在最新版本中已得到修复.

This appears to be a bug in recent versions of Chrome. It was reported on October 14th, 2015 and appears to be fixed in recent builds.

对我来说,更新为Chrome 46.0.2490.80解决了该问题.

For me, updating to Chrome 46.0.2490.80 solved the issue.

来源: https://code.google.com/p/chromium/issues/detail?id = 543018

这篇关于当我使用Controller.File方法时,操作返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆