C# 在浏览器中打开文件而不是下载它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19411335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make a file open in browser instead of downloading it
提问by Trevor
I have an MVC project that will display some documents to users. The files are currently stored in Azure blob storage.
我有一个 MVC 项目,它将向用户显示一些文档。这些文件当前存储在 Azure blob 存储中。
Currently, the documents are retrieved from the following controller action:
目前,文档是从以下控制器操作中检索的:
[GET("{zipCode}/{loanNumber}/{classification}/{fileName}")]
public ActionResult GetDocument(string zipCode, string loanNumber, string classification, string fileName)
{
// get byte array from blob storage
byte[] doc = _docService.GetDocument(zipCode, loanNumber, classification, fileName);
string mimeType = "application/octet-stream";
return File(doc, mimeType, fileName);
}
Right now, when a user clicks on a link like the following:
现在,当用户单击如下链接时:
<a target="_blank" href="http://...controller//GetDocument?zipCode=84016&loanNumber=12345678classification=document&fileName=importantfile.pdf
Then, the file downloads to their browser's downloads folder. What I would like to happen (and I thought was default behavior) is for the file to simply be displayed in the browser.
然后,文件会下载到浏览器的下载文件夹中。我想要发生的(我认为是默认行为)是让文件简单地显示在浏览器中。
I have tried changing the mimetype and changing the return type to FileResult instead of ActionResult, both to no avail.
我尝试更改 mimetype 并将返回类型更改为 FileResult 而不是 ActionResult,两者都无济于事。
How can I make the file display in the browser instead of downloading?
如何使文件显示在浏览器中而不是下载?
采纳答案by Trevor
Thanks to all the answers, the solution was a combination of all of them.
感谢所有的答案,解决方案是所有这些的组合。
First, because I was using a byte[]
the controller action needed to be FileContentResult
not just FileResult
. Found this thanks to: What's the difference between the four File Results in ASP.NET MVC
首先,因为我使用的byte[]
是控制器操作,FileContentResult
而不仅仅是FileResult
. 发现这要归功于:ASP.NET MVC 中的四个文件结果有什么区别
Second, the mime type needed to NOT be a octet-stream
. Supposedly, using the stream causes the browser to just download the file. I had to change the type application/pdf
. I will need to explore a more robust solution to handle other file/mime types though.
其次, mime 类型不需要是octet-stream
. 据说,使用流会导致浏览器只下载文件。我不得不改变类型application/pdf
。不过,我需要探索一个更强大的解决方案来处理其他文件/mime 类型。
Third, I had to add a header that changed the content-disposition
to inline
. Using this postI figured out I had to modify my code to prevent duplicate headers, since the content-disposition was already being set to attachment
.
第三,我必须添加一个content-disposition
将inline
. 使用这篇文章,我发现我必须修改我的代码以防止重复的标题,因为内容处置已经被设置为attachment
.
The successful code:
成功的代码:
public FileContentResult GetDocument(string zipCode, string loanNumber, string classification, string fileName)
{
byte[] doc = _docService.GetDocument(zipCode, loanNumber, classification, fileName);
string mimeType = "application/pdf"
Response.AppendHeader("Content-Disposition", "inline; filename=" + fileName);
return File(doc, mimeType);
}
回答by André Pena
Browsers should decide on downloading or displaying based on mime-type.
浏览器应根据 MIME 类型决定是下载还是显示。
Try this:
尝试这个:
string mimeType = "application/pdf";
回答by welegan
It looks like someone else asked a similar question a while ago:
之前好像有人问过类似的问题:
how to force pdf files to open in a browser
With an answer saying that you should use the header:
有一个回答说你应该使用标题:
Content-Disposition: inline; filename.pdf
回答by Divya
Just return PhysicalFileResult and use HttpGet method ,url will open pdf file
只需返回 PhysicalFileResult 并使用 HttpGet 方法,url 将打开 pdf 文件
public ActionResult GetPublicLink()
{
path = @"D:\Read\x.pdf";
return new PhysicalFileResult(path, "application/pdf");
}