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

前端 未结 5 495
半阙折子戏
半阙折子戏 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:05

    Currently you are saving your file on the file server, thereby overwriting the same pdf with every request. And probably causing errors if you get two requests for a PDF at the same time.

    Use Response to return the PDF (from the memorystream) to the user, and skip the writing of the PDF to a file locally on your server.

    The browser will ask the user where the file should be saved. Something like:

      Response.ContentType = "Application/pdf";
      myMemoryStream.CopyTo(Response.OutputStream);
    

    Also look at the answer from Alun, using content-disposition you can propose a filename to the user.

提交回复
热议问题