C# 如何直接在浏览器中打开 pdf 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14714486/
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
How can I open a pdf file directly in my browser?
提问by Bronzato
I would like to view a PDF
file directly in my browser. I know this question is already asked but I haven't found a solution that works for me.
我想PDF
直接在浏览器中查看文件。我知道这个问题已经有人问过了,但我还没有找到适合我的解决方案。
Here is my action's controller code so far:
到目前为止,这是我的操作的控制器代码:
public ActionResult GetPdf(string fileName)
{
string filePath = "~/Content/files/" + fileName;
return File(filePath, "application/pdf", fileName);
}
Here is my view:
这是我的观点:
@{
doc = "Mode_d'emploi.pdf";
}
<p>@Html.ActionLink(UserResource.DocumentationLink, "GetPdf", "General", new { fileName = doc }, null)</p>
When I mouse hover the link here is the link:
当我鼠标悬停时,这里的链接是链接:
The problem with my code is that the pdf
file is not viewed in the browser but I get a message asking me if I wand to open or save the file.
我的代码的问题pdf
是无法在浏览器中查看该文件,但我收到一条消息,询问我是否要打开或保存该文件。
I know it is possible and my browser support it because I already test it with another website allowing me to view pdf
directly in my browser.
我知道这是可能的,而且我的浏览器支持它,因为我已经在另一个网站上对其进行了测试,允许我pdf
直接在浏览器中查看。
For example, here is the link when I mouse hover a link (on another website):
例如,这是我将鼠标悬停在链接上(在另一个网站上)时的链接:
As you can see there is a difference in the generated link. I don't know if this is useful.
如您所见,生成的链接有所不同。我不知道这是否有用。
Any idea how can I view my pdf
directly in the browser?
知道如何pdf
直接在浏览器中查看我的吗?
采纳答案by ataravati
The accepted answer is wrong. The reason you're getting a message asking you to open or save the file is that you're specifying a filename. If you don't specify the filename the PDF file will be opened in your browser.
接受的答案是错误的。您收到要求您打开或保存文件的消息的原因是您指定了文件名。如果您不指定文件名,PDF 文件将在您的浏览器中打开。
So, all you need to do is to change your action to this:
因此,您需要做的就是将您的操作更改为:
public ActionResult GetPdf(string fileName)
{
string filePath = "~/Content/files/" + fileName;
return File(filePath, "application/pdf");
}
Or, if you need to specify a filename you'll have to do it this way:
或者,如果您需要指定文件名,则必须这样做:
public ActionResult GetPdf(string fileName)
{
string filePath = "~/Content/files/" + fileName;
Response.AddHeader("Content-Disposition", "inline; filename=" + fileName);
return File(filePath, "application/pdf");
}
回答by Forty-Two
Instead of returning a File
, try returning a FileStreamResult
而不是返回 a File
,尝试返回 aFileStreamResult
public ActionResult GetPdf(string fileName)
{
var fileStream = new FileStream("~/Content/files/" + fileName,
FileMode.Open,
FileAccess.Read
);
var fsResult = new FileStreamResult(fileStream, "application/pdf");
return fsResult;
}
回答by TotPeRo
If you read the file stored in database image column, you can use like this:
如果您读取存储在数据库图像列中的文件,您可以像这样使用:
public ActionResult DownloadFile(int id)
{
using (var db = new DbContext())
{
var data =
db.Documents.FirstOrDefault(m => m.ID == id);
if (data == null) return HttpNotFound();
Response.AppendHeader("content-disposition", "inline; filename=filename.pdf");
return new FileStreamResult(new MemoryStream(data.Fisier.ToArray()), "application/pdf");
}
}
回答by Amir
Change your code to this :
将您的代码更改为:
Response.AppendHeader("Content-Disposition","inline;filename=xxxx.pdf");
return File(filePath, "application/pdf");
回答by dush88c
If you are using Rotativapackage to generate PDF, Then don't put a name to file with FileNameattribute like below example.
如果您使用Rotativa包生成 PDF,则不要像下面的示例那样使用FileName属性为文件命名。
return new PartialViewAsPdf("_JcPdfGenerator", pdfModel);
Hope this is helpful to someone.
希望这对某人有帮助。
回答by M...B
Although previous posts are often correct; I think most of them are not best practice!
I'd like to suggest to change action return types to FileContentResult
and usereturn new FileContentResult(fileContent, "application/pdf");
at the end of action body.
尽管以前的帖子通常是正确的;我认为他们中的大多数都不是最佳实践!我想建议将动作返回类型更改为FileContentResult
并return new FileContentResult(fileContent, "application/pdf");
在动作主体的末尾使用。
回答by sathish v
Yes You Can do It Simply by redirecting . it ends extension like u need , .pdf ..
是的,您可以简单地通过重定向来做到这一点。它像你需要的那样结束扩展,.pdf ..
protected void OpenPdfPdf_Click(object sender, EventArgs e)
{
Response.Redirect("jun.pdf");
}
Or another Method ,its opens like .aspx page--
或另一种方法,它像 .aspx 页面一样打开--
protected void OpenPdf_Click(object sender, EventArgs e)
{
string path = Server.MapPath("jun.pdf");
//or you want to load from url change path to
//string path="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);
if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
}
}