Trouble downloading PhantomJS generated pdf

混江龙づ霸主 提交于 2019-12-22 10:26:34

问题


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?

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 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 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");
    }

回答1:


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");

Also set response encoding in PhantomJS before writing document body:

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


来源:https://stackoverflow.com/questions/17506210/trouble-downloading-phantomjs-generated-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!