Storing generated PDF files on the server

前端 未结 5 2090
别跟我提以往
别跟我提以往 2020-12-05 05:34

Using the jsPDF plugin, I am creating a .pdf file and generating a download based on a button click. I would like to save the file onto my server i

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-05 06:23

    Using Asp.net/C# and jQuery: Based on @mahmoud hemdan answer, i did for my requirement. I did like below:

    Javascript:

    function createPDF(){
      var doc = new jsPDF('landscape');
      var u = $("#myCanvas").toDataURL("image/png", 1.0);
      doc.addImage(u, 'JPEG', 20, 20, 250, 150);
      var base64pdf = btoa(doc.output());
      $.ajax({
          url: 'ChartPDF.aspx?pdfinput=1',
          type: 'post',
          async: false,
          contentType: 'application/json; charset=utf-8',
          data: base64pdf,
          dataType: 'json'
      });
    }
    

    ChartPDF.aspx.cs code:

    protected void Page_Load(object sender, EventArgs e)
        {
            var pdfinput= Request.QueryString["pdfinput"];
            if (pdfinput!= null)
            {
                var jsonString = string.Empty;
                HttpContext.Current.Request.InputStream.Position = 0;
                using (var inputStream = new StreamReader(Request.InputStream))
                {
                    jsonString = inputStream.ReadToEnd();
                }
                var byteArray = Convert.FromBase64String(jsonString);
                //Get your desired path for saving file
                File.WriteAllBytes(@"C:\ReportPDFs\Test.pdf", byteArray);
            }
        }
    

    I called up the page without any querystring parameter and the page creates the graph(Code not given for drawing the graph as it is not part of the qs.) and after that it calles createPDF() function which result in reloading the page and saving the graph as pdf file on server. Later i use the file from path.

提交回复
热议问题