更改下载文件名 ItextSharp

Change download-file name ItextSharp

所以我一直在使用 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());
    }

您可以添加 content-disposition header 和文件名到响应 object

Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);