Google chrome Chrome和Firefox中的MVC 5内容配置错误
我想在浏览器中以pdf格式显示系统的用户手册 下面的代码在IE9中运行良好,但不是 Chrome-错误从服务器接收到重复错误 Firefox-损坏的内容错误 MVC5代码(我认为添加了IE可以处理的重复头) 我只是想知道有没有什么方法可以适用于所有浏览器Google chrome Chrome和Firefox中的MVC 5内容配置错误,google-chrome,firefox,model-view-controller,Google Chrome,Firefox,Model View Controller,我想在浏览器中以pdf格式显示系统的用户手册 下面的代码在IE9中运行良好,但不是 Chrome-错误从服务器接收到重复错误 Firefox-损坏的内容错误 MVC5代码(我认为添加了IE可以处理的重复头) 我只是想知道有没有什么方法可以适用于所有浏览器 public FileResult UserManual() { var FileName = "user-manual.pdf"; var cd = new ContentDisposition
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);
}
我知道这是老问题,但我今天也遇到了同样的问题 如果添加“内容处置”标题,则不要返回添加了“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
希望这有帮助…为了在浏览器中显示文件,请不要在return语句中将文件名作为file方法的第三个参数。这将强制在后台对附件进行内容处理。同样地,您的代码应该会导致一个无效的响应,并出现ERR\u response\u HEADERS\u MULTIPLE\u CONTENT\u DISPOSITION错误