javascript - JSPDF 保存在本地系统上,但我想将它保存在服务器上

标签 javascript jquery jspdf

我正在使用 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);
        },
    });
}

最佳答案

我可以用 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;
        }
    }

}

关于javascript - JSPDF 保存在本地系统上,但我想将它保存在服务器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47867930/

相关文章:

javascript - 未捕获的 DOMException : Failed to execute 'toDataURL' on 'HTMLCanvasElement' : Tainted canvases may not be exported

jquery - 无法使用 jQuery 获取数据属性的值

javascript - IE 使用 html2canvas 返回空/空白 Canvas

javascript - 使用 jsPDF 用 JSON 数据填充 PDF

javascript - 如何将点击绑定(bind)到父级没有类的元素?

javascript - 调整文本autotable jspdf

javascript - react js 谷歌翻译不工作

javascript - 是否有任何类似于 Whatever :hover htc? 的 jquery 插件或技术

javascript - 如何在 OpenCart 中延迟或异步 javascript

javascript - 传递给 jQuery 的 one() 方法的函数仅在第一次触发事件时执行