我正在使用下面的代码,这些代码用于以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文件,我想做的是:
我需要像使用Response.Write()
和Response.End()
等在asp.net中一样在浏览器上下载它。
我可以在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");
答案 0 :(得分:1)
我将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");
}
}
答案 1 :(得分: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();
//...
}
答案 2 :(得分:1)
您可以下载三向文件。
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>
答案 3 :(得分:0)
您还可以使用ITextSharp或Jspdf下载pdf。