【发布时间】: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");
【问题讨论】:
-
如果您想在浏览器中显示 PDF(而不是提示将其保存在磁盘上),那么服务器端代码需要指定
Content-Disposition: Inline标头。例如,请参阅ASP.Net Core Content-Disposition attachment/inline。 -
@AlwaysLearning 试过这个:stackoverflow.com/a/38909848/7124761 但在 chrome 上没有任何东西
-
你用的是什么asp.net核心项目?剃刀页面? mvc?
-
@Harry 它的 MVC..
标签: c# asp.net-core asp.net-core-mvc