I want to make the browser download a PDF document from server instead of opening the file in browser itself. I am using C#.
我想让浏览器从服务器下载PDF文档,而不是在浏览器中打开文件。我正在使用C#。
Below is my sample code which I used. It not working..
下面是我使用的示例代码。它不工作..
string filename = "Sample server url";
response.redirect(filename);
4 个解决方案
#1
30
You should look at the "Content-Disposition" header; for example setting "Content-Disposition" to "attachment; filename=foo.pdf" will prompt the user (typically) with a "Save as: foo.pdf" dialog, rather than opening it. This, however, needs to come from the request that is doing the download, so you can't do this during a redirect. However, ASP.NET offers Response.TransmitFile
for this purpose. For example (assuming you aren't using MVC, which has other preferred options):
你应该看一下“Content-Disposition”标题;例如,将“Content-Disposition”设置为“attachment; filename = foo.pdf”将提示用户(通常)使用“另存为:foo.pdf”对话框,而不是打开它。但是,这需要来自正在进行下载的请求,因此在重定向期间无法执行此操作。但是,ASP.NET为此提供了Response.TransmitFile。例如(假设您没有使用MVC,它有其他首选选项):
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=foo.pdf");
Response.TransmitFile(filePath);
Response.End();
#2
4
If you want to render the file(s) so that you could save them at your end instead of opening in the browser, you may try the following code snippet:
如果要渲染文件以便可以将它们保存在最终而不是在浏览器中打开,则可以尝试以下代码段:
//create new MemoryStream object and add PDF file’s content to outStream.
MemoryStream outStream = new MemoryStream();
//specify the duration of time before a page cached on a browser expires
Response.Expires = 0;
//specify the property to buffer the output page
Response.Buffer = true;
//erase any buffered HTML output
Response.ClearContent();
//add a new HTML header and value to the Response sent to the client
Response.AddHeader(“content-disposition”, “inline; filename=” + “output.pdf”);
//specify the HTTP content type for Response as Pdf
Response.ContentType = “application/pdf”;
//write specified information of current HTTP output to Byte array
Response.BinaryWrite(outStream.ToArray());
//close the output stream
outStream.Close();
//end the processing of the current page to ensure that no other HTML content is sent
Response.End();
However, if you want to download the file using a client application then you'll have to use the WebClient class.
但是,如果要使用客户端应用程序下载文件,则必须使用WebClient类。
#3
2
I use this by setting inline parameter to true it will show in browser false it will show save as dialog in browser.
我通过将inline参数设置为true来使用它,它将在浏览器中显示false,它将在浏览器中显示另存为对话框。
public void ExportReport(XtraReport report, string fileName, string fileType, bool inline)
{
MemoryStream stream = new MemoryStream();
Response.Clear();
if (fileType == "xls")
report.ExportToXls(stream);
if (fileType == "pdf")
report.ExportToPdf(stream);
if (fileType == "rtf")
report.ExportToRtf(stream);
if (fileType == "csv")
report.ExportToCsv(stream);
Response.ContentType = "application/" + fileType;
Response.AddHeader("Accept-Header", stream.Length.ToString());
Response.AddHeader("Content-Disposition", String.Format("{0}; filename={1}.{2}", (inline ? "Inline" : "Attachment"), fileName, fileType));
Response.AddHeader("Content-Length", stream.Length.ToString());
//Response.ContentEncoding = System.Text.Encoding.Default;
Response.BinaryWrite(stream.ToArray());
Response.End();
}
#4
0
They are almost same in most of the cases but there is a difference:
它们在大多数情况下几乎相同,但存在差异:
Add Header will replace the previous entry with the same key
添加标题将使用相同的键替换上一个条目
Append header will not replace the key, rather will add another one.
追加标题不会替换密钥,而是会添加另一个。
#1
30
You should look at the "Content-Disposition" header; for example setting "Content-Disposition" to "attachment; filename=foo.pdf" will prompt the user (typically) with a "Save as: foo.pdf" dialog, rather than opening it. This, however, needs to come from the request that is doing the download, so you can't do this during a redirect. However, ASP.NET offers Response.TransmitFile
for this purpose. For example (assuming you aren't using MVC, which has other preferred options):
你应该看一下“Content-Disposition”标题;例如,将“Content-Disposition”设置为“attachment; filename = foo.pdf”将提示用户(通常)使用“另存为:foo.pdf”对话框,而不是打开它。但是,这需要来自正在进行下载的请求,因此在重定向期间无法执行此操作。但是,ASP.NET为此提供了Response.TransmitFile。例如(假设您没有使用MVC,它有其他首选选项):
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=foo.pdf");
Response.TransmitFile(filePath);
Response.End();
#2
4
If you want to render the file(s) so that you could save them at your end instead of opening in the browser, you may try the following code snippet:
如果要渲染文件以便可以将它们保存在最终而不是在浏览器中打开,则可以尝试以下代码段:
//create new MemoryStream object and add PDF file’s content to outStream.
MemoryStream outStream = new MemoryStream();
//specify the duration of time before a page cached on a browser expires
Response.Expires = 0;
//specify the property to buffer the output page
Response.Buffer = true;
//erase any buffered HTML output
Response.ClearContent();
//add a new HTML header and value to the Response sent to the client
Response.AddHeader(“content-disposition”, “inline; filename=” + “output.pdf”);
//specify the HTTP content type for Response as Pdf
Response.ContentType = “application/pdf”;
//write specified information of current HTTP output to Byte array
Response.BinaryWrite(outStream.ToArray());
//close the output stream
outStream.Close();
//end the processing of the current page to ensure that no other HTML content is sent
Response.End();
However, if you want to download the file using a client application then you'll have to use the WebClient class.
但是,如果要使用客户端应用程序下载文件,则必须使用WebClient类。
#3
2
I use this by setting inline parameter to true it will show in browser false it will show save as dialog in browser.
我通过将inline参数设置为true来使用它,它将在浏览器中显示false,它将在浏览器中显示另存为对话框。
public void ExportReport(XtraReport report, string fileName, string fileType, bool inline)
{
MemoryStream stream = new MemoryStream();
Response.Clear();
if (fileType == "xls")
report.ExportToXls(stream);
if (fileType == "pdf")
report.ExportToPdf(stream);
if (fileType == "rtf")
report.ExportToRtf(stream);
if (fileType == "csv")
report.ExportToCsv(stream);
Response.ContentType = "application/" + fileType;
Response.AddHeader("Accept-Header", stream.Length.ToString());
Response.AddHeader("Content-Disposition", String.Format("{0}; filename={1}.{2}", (inline ? "Inline" : "Attachment"), fileName, fileType));
Response.AddHeader("Content-Length", stream.Length.ToString());
//Response.ContentEncoding = System.Text.Encoding.Default;
Response.BinaryWrite(stream.ToArray());
Response.End();
}
#4
0
They are almost same in most of the cases but there is a difference:
它们在大多数情况下几乎相同,但存在差异:
Add Header will replace the previous entry with the same key
添加标题将使用相同的键替换上一个条目
Append header will not replace the key, rather will add another one.
追加标题不会替换密钥,而是会添加另一个。