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

时间:2017-12-18 11:52:21

标签: 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);
        },
    });
}

1 个答案:

答案 0 :(得分:0)

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

}