iTextSharp generated PDF: How to send the pdf to the client and add a prompt?

前端 未结 5 496
半阙折子戏
半阙折子戏 2020-12-01 17:25

I have generated a pdf using iTextSharp, when its created it saves automatically in the location provided in my code on the server not on the client side and of course witho

5条回答
  •  难免孤独
    2020-12-01 18:06

    In case of a web application you probably want to stream the pdf as binary to user, that would either open the pdf or prompt user to save the file.

    Remember pdf generation is happening at server, even if user provides the path it won't be of any use on server. See following links -

    • How To Write Binary Files to the Browser Using ASP.NET and Visual C# .NET

    In your case you are generating the file and hence will already be having a binary stream instead of file, hence you can directly use Response.BinaryWrite instead of Response.WriteFile.

    Modified sample:

    Response.Buffer = false;
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    //Set the appropriate ContentType.
    Response.ContentType = "Application/pdf";
    //Write the file content directly to the HTTP content output stream.
    Response.BinaryWrite(content);
    Response.Flush();
    Response.End();
    

提交回复
热议问题