无法下载PhantomJS生成的pdf [英] Trouble downloading PhantomJS generated pdf

查看:165
本文介绍了无法下载PhantomJS生成的pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将PhantomJS生成的文件下载到我的asp.net应用程序时遇到麻烦. 我正在将phantomJs作为服务器运行: 该文件已正确生成并保存到PhantomJS文件夹的磁盘中,但是在传输并包含到我的Http响应流中时发生了一些情况.该文件是由浏览器下载的,但是当我尝试打开它时,文件错误并显示消息无法打开.我怀疑它在流传输中被损坏了吗?

I am having some trouble downloading a PhantomJS generated file to my asp.net application. I am running phantomJs as a server: The file is generated correctly and saved to disk in the PhantomJS folder, but something happens during the transfer and inclusion into my Http response stream. The file is downloaded by the browser, but when I try to open it, the file errors with the message that it can't be opened. I suspect it gets corrupted in the stream transfer?

我想避免从文件系统中存储的位置读取pdf,而是从PhantomJS返回的响应中获取

I would like to avoid reading the pdf from its stored location in the file system, and instead get it out of the response returned from PhantomJS

PhantomJS代码:

PhantomJS code:

page.open(url, function (status) {

    page.render(fullFileName);

    var fs = require('fs');

    var pdfContent = fs.read(fullFileName);
    response.statusCode = 200;
    response.headers = {
                    'Cache': 'no-cache',
                    'Content-Type': 'application/pdf',
                    'Connection': 'Keep-Alive',
                    'Content-Length': pdfContent.length
                    };

     response.setEncoding("binary");
     response.write(pdfContent);


});

ASP.NET代码:

ASP.NET code:

public ActionResult DownloadPdfFromUrl(ConvertionModel model)
    {
        HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("http://localhost:8080");

        ASCIIEncoding encoding = new ASCIIEncoding();
        string postData = string.Format("url={0}&fileName=myTest&extension=pdf&format=pdf", model.UrlToConvertPdf);
        byte[] data = encoding.GetBytes(postData);

        httpWReq.Method = "POST";
        httpWReq.ContentType = "application/x-www-form-urlencoded";
        httpWReq.ContentLength = data.Length;

        using (Stream s = httpWReq.GetRequestStream())
        {
            s.Write(data, 0, data.Length);
        }

        var response = (HttpWebResponse)httpWReq.GetResponse();

        Response.AppendHeader("Content-Disposition", "attachment;filename=test.pdf");

        return new FileStreamResult(response.GetResponseStream(), "application/pdf");
    }

推荐答案

C#代码看起来不错,但最好不要返回空操作结果.我最好写

The C# code looks good, but it's better not to return null action result. I'd better write

var stream = response.GetResponseStream();

var buffer = new byte[response.ContentLength];
stream.Read(buffer, 0, buffer.Length);

return File(buffer, "application/pdf", "test.pdf");

还要在编写文档正文之前在PhantomJS中设置响应编码:

Also set response encoding in PhantomJS before writing document body:

response.setEncoding("binary");
response.write(pdfContent);

这篇关于无法下载PhantomJS生成的pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆