【问题标题】:JSPDF is save on the local system but i want to save it on the serverJSPDF 保存在本地系统上,但我想将其保存在服务器上
【发布时间】:2018-09-24 20:20:42
【问题描述】:

我正在使用 JSPDF 来保存 PDF 文件。我知道了,但下一个任务是将我的 PDF 保存在服务器文件夹上,而不是我的本地计算机上。我的功能是:

function generatePDFFile(elem) {
    var date = new Date();
    var filename = date.getUTCDate()+date.getTime();
    const el = document.querySelector('#divInvoiceDLG')
    el.scrollIntoView()
    $(el).css("overflow", "hidden");
    html2canvas(el, {
        useCORS: true,
        allowTaint: true,
        letterRendering: true,
        logging: true,
        onrendered: function (canvas) {


            var quality = [0.0, 1.0];
            var img = canvas.toDataURL("img/png",quality);
            var doc1 = new jsPDF();
            doc1.addImage(img, "JPEG", 1, 1);
            doc1.save( filename);
        },
    });
}

【问题讨论】:

  • 使用一些服务器端 PDF 库,具体取决于您的后端语言。 (我正在使用fpdf
  • 要么在服务器端生成文档(假设您可以在流程中的某个时间点在服务器端重现相同的内容)。或者找出是否可以获取 doc1 的内容并将其序列化以通过 ajax 请求传输到服务器,而不是调用 save() 方法。
  • 您可以使用表单数据执行此操作。请参考stackoverflow.com/questions/29826687/…的答案。
  • @MitBhatt 此代码与 PHP 有关,但我正在使用 asp.net 和 jquery。
  • @AliImran 如果不相关,您可以忽略服务器端部分。但是既然你问如何上传文件,那么 Mit 建议的 JS 部分显然是相关的。上传后如何在 ASP.NET 中处理它是一个单独的过程。

标签: javascript jquery jspdf


【解决方案1】:

我可以用 c# 代码做到这一点。

%@ WebHandler Language="C#" Class="Attachments" %>

using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

public class Attachments : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Expires = -1;
        try
        {
            HttpPostedFile postedFile = context.Request.Files[0];
            string PracticeId = HttpContext.Current.Profile.GetPropertyValue("PracticeId").ToString();
            string directory = HttpContext.Current.Request.Form["Directory"];

            Guid newGuid = Guid.NewGuid();
            string savepath = context.Server.MapPath(ConfigurationManager.AppSettings["DocumentsPath"] + "/" + PracticeId + "/" + directory);            

            if (!Directory.Exists(savepath))
            {
                Directory.CreateDirectory(savepath);
            }
            string filename = newGuid + "." + postedFile.FileName.Split('.').Last();
            postedFile.SaveAs(savepath + @"\" + filename);

            ResponseFile objResponse = new ResponseFile
                                            {
                                                path = newGuid + "." + filename.Split('.').Last(),
                                                fileName = postedFile.FileName
                                            };

            var jSon = new JavaScriptSerializer();
            var outPut = jSon.Serialize(objResponse);
            context.Response.Write(outPut);         
        }
        catch (Exception ex)
        {
            context.Response.Write("Error: " + ex.Message);
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-16
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多