Prashant Pimpale
Prashant Pimpale

Reputation: 10697

How to download PDF file on browser in Asp.net CORE MVC

I am using below code which used to download SSRS report in pdf format:

 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();

Which perfectly creates a pdf file in the specified path E:\New folder\, what I am trying to do is:

I need download it on browser as we were doing in the asp.net with Response.Write() and Response.End() etc.

Can I do the same in the ASP.Net Core?

What I 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"); 

Upvotes: 2

Views: 28163

Answers (3)

nuuSoftechnology
nuuSoftechnology

Reputation: 1

You can download a three-way file.

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>

enter image description here

Upvotes: 2

Rena
Rena

Reputation: 36565

First,you need to be sure that you have uploaded the file in your project.

Here is a simple demo about how to download pdf on the browser in Asp.Net Core:

1.View:

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

2.Controller(be sure that the file have been exsit in wwwroot/file folder):

 [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.Result: enter image description here

Upvotes: 8

Harry
Harry

Reputation: 4050

I copied my pdf into the solution and changed the copy to output directory property to copy.

index.cshtml which opens the pdf in a browser tab:

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

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

index.cshtml which downloads the pdf to file system (include the download attribute on the html anchor tag):

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

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

home controller:

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

Upvotes: 3

Related Questions