所以我一直在使用 itextsharp 创建文件并将其下载到客户端。PDF 已创建,但创建时使用了错误的文件扩展名。它下载为“webform1.aspx”,但如果我更改文件扩展名,它是正确的。我需要学习如何在使用内存流下载时更改文件名,或者如果需要的话使用不同的方法。下面的代码是通过空白网络表单上的按钮执行的。
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());
}
Run Code Online (Sandbox Code Playgroud)
您可以将带有文件名的内容处置标头添加到响应对象
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Run Code Online (Sandbox Code Playgroud)