首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在selectPDF中使用ajax

在selectPDF中使用ajax
EN

Stack Overflow用户
提问于 2018-06-19 23:25:29
回答 1查看 294关注 0票数 0

我有以下ajax调用:

代码语言:javascript
复制
$.ajax({
    url: 'WebService.asmx/ConvertPDF',
    data: "{'section':'<html><head></head><body>Ajax html</body></html>'}",
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    dataType: 'JSON',
    async: false,
    success: function (response) {
        // proceed
    },
    error: function () {
        // fail code
    }
});

和the服务

代码语言:javascript
复制
[WebMethod(EnableSession = true)]
public void ConvertPDF(string section) {
    HtmlToPdf convertor = new HtmlToPdf();
    string _html = "<html><head></head><body><p>this is a test</p></body></html";

    string size = "A4", orientation = "Portrait";
    PdfPageSize pdfSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), size, true);
    PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), orientation, true);
    convertor.Options.PdfPageSize = pdfSize;
    convertor.Options.PdfPageOrientation = pdfOrientation;
    convertor.Options.WebPageWidth = 1024;
    convertor.Options.MinPageLoadTime = 2;
    convertor.Options.WebPageHeight = 0;

    PdfDocument doc = convertor.ConvertHtmlString(_html, "");
    doc.Save(HttpContext.Current.Response, false, "Sample.pdf");
    doc.Close();
}

在本地运行服务方法可以很好地工作,但是当我通过JS按钮调用ajax时,我得到“Thread was aborted”。

有什么办法绕过它吗?基本上,按钮从页面上的部分抓取html,并(最终)将其传递给方法以输出到PDF,本质上是用部分参数中的内容替换_html变量。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2019-11-21 15:22:33

//使用selectpdf将Html转换为pdf的Post方法

代码语言:javascript
复制
[HttpPost]
    public ActionResult ConvertDocument(MyDocumentDTO model)
    {

        HtmlToPdf converter = new HtmlToPdf();

        PdfDocument doc = converter.ConvertHtmlString(model.Context);
        // save pdf document
        byte[] pdf = doc.Save();

        string handle= Guid.NewGuid().ToString();
        TempData[handle] = pdf;
        return Json(handle, JsonRequestBehavior.AllowGet);

    }

Ajax调用

代码语言:javascript
复制
        $.ajax({
            url: '/document/ConvertDocument',
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: ko.toJSON(document),
            success: function (data) {
                window.location = 'download?fileGuid=' + data;
            },

// Get文件下载请求

代码语言:javascript
复制
    [HttpGet]
    public ActionResult Download(string fileGuid)
    {
        byte[] pdf = TempData[fileGuid] as byte[];
        FileResult fileResult = new FileContentResult(pdf, "application/pdf");
        fileResult.FileDownloadName = "Document.pdf";
        return fileResult;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50931972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档