【问题标题】:How to download PDF file on browser in Asp.net CORE MVC如何在 Asp.net CORE MVC 中的浏览器上下载 PDF 文件
【发布时间】:2020-02-07 13:10:47
【问题描述】:

我正在使用以下代码下载 pdf 格式的 SSRS 报告:

 string URL = "http://ssrs-test.com/ReportS/?/UAT_FOLDER";
 URL = URL + "&SRNo=122&rs:Command=Render&rs:Format=pdf";

 System.Net.HttpWebRequest Req = (System.Net.HttpWebRequest) System.Net.WebRequest.Create(URL);

 Req.Method = "GET";
 string path = @ "E:\New folder\Test.pdf";
 System.Net.WebResponse objResponse = Req.GetResponse();
 System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create);
 System.IO.Stream stream = objResponse.GetResponseStream();
 byte[] buf = new byte[1024];
 int len = stream.Read(buf, 0, 1024);

 while (len > 0) {
  fs.Write(buf, 0, len);
  len = stream.Read(buf, 0, 1024);
 }
 stream.Close();
 fs.Close();

在指定路径E:\New folder\中完美创建pdf文件,我想做的是:

我需要在浏览器上下载它,就像我们在 asp.net 中使用 Response.Write()Response.End() 等一样。

我可以在 ASP.Net Core 中做同样的事情吗?

tried

return new PhysicalFileResult(@"with_samplepdf_file", "application/pdf");  -- Not worked

var stream = new FileStream(@"with_samplepdf_file", FileMode.Open);
return new FileStreamResult(stream, "application/pdf");   -- Not worked - Nothing happening on the browser

var file = @"with_samplepdf_file/pdf";

// Response...
System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
{
   FileName = file,
   Inline = displayInline  // false = prompt the user for downloading;  true = browser to try to show the file inline
};
Response.Headers.Add("Content-Disposition", cd.ToString());
Response.Headers.Add("X-Content-Type-Options", "nosniff");

return File(System.IO.File.ReadAllBytes(file), "application/pdf"); 

【问题讨论】:

标签: c# asp.net-core asp.net-core-mvc


【解决方案1】:

首先,您需要确保您已将文件上传到您的项目中。

这里有一个简单的演示,介绍如何在 Asp.Net Core 中的浏览器上下载 pdf:

1.查看:

<a asp-action="GetPdf" asp-controller="Users">Download</a>

2.Controller(确保文件已经存在于wwwroot/file文件夹中):

 [HttpGet]
 public ActionResult GetPdf()
 {
     string filePath = "~/file/test.pdf";
     Response.Headers.Add("Content-Disposition", "inline; filename=test.pdf");
     return File(filePath, "application/pdf");           
 }

3.Startup.cs:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
   //...
   app.UseStaticFiles();
   //...
}

4.结果:

【讨论】:

  • 项目中是否可以不存储pdf?
  • 只有确保文件存在于服务器上才能找到文件路径并下载。
  • 可以用Stream吗?这意味着无需存储文件
  • 虽然是用stream做的,但也需要在server端存在。
【解决方案2】:

我将我的 pdf 复制到解决方案中,并将复制到输出目录属性更改为复制。

index.cshtml 在浏览器标签中打开 pdf:

@{
    ViewData["Title"] = "Home Page";
}

<a href="/home/get">Open PDF</a>

index.cshtml 下载 pdf 到文件系统(包括 html 锚标记上的下载属性):

@{
    ViewData["Title"] = "Home Page";
}

<a href="/home/get" download>Download PDF</a>

家庭控制器:

public class HomeController : Controller
{
    public IActionResult Get()
    {
        var stream = new FileStream(@"example.pdf", FileMode.Open);
        return new FileStreamResult(stream, "application/pdf");
    }
}

【讨论】:

  • 我正在使用 AspNet Core,让我试试这个"
  • 这是 aspnet 核心
  • 我不知道为什么调用 get 方法但不工作,因为 Ajax 会有所作为吗?
  • 使用 ajax 时,响应会在您的 javascript 中的 http 响应中返回。您将需要使用 javascript 来解析响应并手动下载。像这样ourcodeworld.com/articles/read/545/…
  • 实际上可能链接不好。我只是快速搜索它。也许看看这个stackoverflow.com/questions/1999607/…
【解决方案3】:

您可以下载三向文件。

public async Task<IActionResult> Get()
{
    var path = Path.Combine(
    Directory.GetCurrentDirectory(), "wwwroot\\images\\4.pdf");

    var memory = new MemoryStream();
    using (var stream = new FileStream(path, FileMode.Open))
    {
       await stream.CopyToAsync(memory);
    }
    memory.Position = 0;
    return File(memory, "application/pdf", "Demo.pdf");
}

HTML;

<form asp-controller="pdf" asp-action="Get" method="get">
  <button type="submit">Download</button>
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 2014-07-09
    • 2017-07-17
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    相关资源
    最近更新 更多