Chrome和Firefox中的MVC 5内容处理错误

时间:2014-12-11 15:41:22

标签: google-chrome firefox model-view-controller

我想在浏览器中以pdf格式显示系统的用户手册。

以下代码在IE9中可以正常工作,但不能正常工作

Chrome - 从服务器收到错误重复错误

Firefox - 损坏的内容错误

MVC 5代码(我认为正在添加IE可以处理的重复标题)

只是想知道这有什么办法适用于所有浏览器?

public FileResult UserManual()
    {
        var FileName = "user-manual.pdf";
        var cd = new ContentDisposition
        {
            Inline = true,
            FileName = FileName
        };
        Response.AddHeader("Content-Disposition", cd.ToString());
        string path = AppDomain.CurrentDomain.BaseDirectory + "App_Data/";
        return File(path + FileName, MediaTypeNames.Application.Pdf, FileName);

    }

2 个答案:

答案 0 :(得分:3)

我知道这已经过时了,但今天我遇到了同样的问题。

如果添加“Content-Disposition”标题,则不要返回添加了“FileName”参数的文件

检查一下:

if (myFile.IsInlineContent()) // Evaluate the content type to check if the file can be shown in the browser
{
    Response.AppendHeader("content-disposition", $"inline; myFile.Filename}");
    return File(myFile.Path, myFile.Type); // return without filename argument
} 
// if not, treat it as a regular download
return File(myFile.Path, myFile.Type, myFile.Filename); // return with filename argument

希望这会有所帮助......

答案 1 :(得分:1)

为了在浏览器中显示文件,请不要在return语句中提供文件名作为File方法的第三个参数。这迫使附件的内容处理方式在幕后。因此,您的代码应导致响应无效,并显示ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION错误。