C# 更改下载文件名ItextSharp
所以我一直在使用itextsharp创建一个文件并下载到客户端。PDF已创建,但使用错误的文件扩展名创建。它被下载为“webform1.aspx”,但如果我更改了文件扩展名,它是正确的。我需要学习如何在使用内存流下载时更改文件名,或者在需要时使用其他方法。在下面的代码中,它是通过空白webform上的按钮执行的C# 更改下载文件名ItextSharp,c#,itextsharp,C#,Itextsharp,所以我一直在使用itextsharp创建一个文件并下载到客户端。PDF已创建,但使用错误的文件扩展名创建。它被下载为“webform1.aspx”,但如果我更改了文件扩展名,它是正确的。我需要学习如何在使用内存流下载时更改文件名,或者在需要时使用其他方法。在下面的代码中,它是通过空白webform上的按钮执行的 protected void Button1_Click(object sender, EventArgs e) { // Create a Document o
protected void Button1_Click(object sender, EventArgs e)
{
// Create a Document object
Document document = new Document(PageSize.A4, 50, 50, 25, 25);
// Create a new PdfWriter object, specifying the output stream
MemoryStream output = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, output);
// Open the Document for writing
document.Open();
// Create a new Paragraph object with the text, "Hello, World!"
var welcomeParagraph = new Paragraph("Hello, World!");
// Add the Paragraph object to the document
document.Add(welcomeParagraph);
document.Close();
Response.ContentType = "pdf/application";
Response.BinaryWrite(output.ToArray());
}
您可以将带有文件名的内容处置标头添加到响应对象
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
您可以将带有文件名的内容处置标头添加到响应对象
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
我以为我已经弄明白了。。。我将响应内容更改为response.ContentType=“Application/pdf”这会将其作为客户端的页面打开,并显示为pdf,但文件类型仍然是aspx。奇怪,我想我已经弄明白了。。。我将响应内容更改为response.ContentType=“Application/pdf”这会将其作为客户端的页面打开,并显示为pdf,但文件类型仍然是aspx。奇怪,整洁。我来试试,效果很好。非常感谢,太好了。我来试试,效果很好。非常感谢你。