【问题标题】:Telerik kendo ui drawing exportPDF send to serverTelerik kendo ui绘图导出PDF发送到服务器
【发布时间】:2016-05-10 04:03:43
【问题描述】:

我在 kendo ui (here) 中使用此绘图功能

为用户导出 PDF 效果很好

但我想使用 AJAX 将 PDF 提交回服务器。

为什么我想要它? 我想允许客户从我的服务器发送带有 PDF 的电子邮件

【问题讨论】:

    标签: javascript pdf kendo-ui kendo-asp.net-mvc


    【解决方案1】:

    这里是向服务器发送请求的代码:

      kendo.drawing.drawDOM($("#statement-print-area"))
                      .then(function (group) {
                          // Render the result as a PDF file
                          return kendo.drawing.exportPDF(group, {
                              paperSize: "auto",
                              margin: { left: "1cm", top: "1cm", right: "1cm", bottom: "1cm" }
                          });
                      })
                      .done(function (data) {
                          $.ajax({
                              type: "POST",
                              url: "/Customer/EmailStatement",
                              processData: false,  // tell jQuery not to process the data
                              contentType: false,  // tell jQuery not to set contentType
                              data: '{ "docData" : "' + data + '" ,"email": "' + vm.sendEmailTo + '"}',
                              contentType: 'application/json; charset=utf-8',
                              dataType: 'json',
                          }).done(handleResponseInfo).fail(ajaxError);
                      });
    

    这是服务器端的代码(asp.net MVC)

                docData = docData.Replace("data:application/pdf;base64,", "");
                byte[] bytes = Convert.FromBase64String(docData);
                Stream stream = new MemoryStream(bytes);
    new Attachment(stream, "Customer statement.pdf", "application/pdf")
    
                    message.Attachments.Add(attachment);
    

    【讨论】:

      【解决方案2】:

      这是另一个例子:

      <script>
      
          $(function () {
              $("#button0PDF").kendoButton();
              var button = $("#button0PDF").data("kendoButton");
      
              button.bind("click", function (e) {
                  kendo.drawing.drawDOM("#divId", {
                      forcePageBreak: ".page-break",
                      //template: $("#page-template").html()
      
                  })
                  .then(function (group) {
                      // Render the result as a PDF file
                      return kendo.drawing.exportPDF(group, {
                          landscape: false
                      });
                  })
                  .done(function (data) {
                      // Save the PDF file
                      kendo.saveAs({
                          dataURI: data,
                          fileName: "fileName.pdf",
                          proxyURL: "/API/Main/Post",
                      });
                  });
              });
          });
      
      </script>
      

      以及服务器端部分:

      public HttpResponseMessage Post([FromBody]FileData file)
              {
                  var data = Convert.FromBase64String(file.Base64);
      
                  var result = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      Content = new StreamContent(new MemoryStream(data))
                  };
      
                  result.Content.Headers.ContentType = new MediaTypeHeaderValue(file.ContentType);
      
                  result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                  {
                      FileName = file.FileName
                  };
      
                  return result;
              }
      

      【讨论】:

      • 你能告诉我什么是代理Uri吗?。我使用spring mvc作为支持。你能告诉我将pdf数据接收到spring mvc控制器的方法,以便我可以将它保存到数据库中。当我在 controller.java 中收到文件格式是什么?
      • 你在服务器端方法中使用 [FromBody] 的东西。你能告诉我这是什么吗?
      • @SachinHR 你的控制器是什么?在我的情况下是'Main',方法是'Post',这就是为什么proxyURL:“/API/Main/Post”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-23
      • 2015-01-23
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      • 1970-01-01
      相关资源
      最近更新 更多