Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        2年前关闭。
                                                                                            
                
        
我正在尝试将HTML转换为PDF文件。

我有以下控制器:

    public ActionResult Offer([FromBody] DocumentTemplateInvoiceViewModel vm)
    {
        return this.Pdf(nameof(Offer), vm, "test.pdf");
    }


当我在这里执行POST时,我取回文件并且它可以打开。快乐的时光!

但是,如果我尝试执行以下操作:

    var termsClient = new RestClient(ConfigurationManager.AppSettings["HostingUrl"]);
    var termsRequest = new RestRequest("/Document/Offer", Method.POST);
    termsRequest.AddJsonBody(vm);
    var json = JsonConvert.SerializeObject(vm);
    var termsBytes = termsClient.DownloadData(termsRequest);
    File.WriteAllBytes("LOCALPATH",termsBytes );


该文件已损坏,我无法打开PDF。它有一些大小,因此它存储一些字节。可能只是没有正确存储:D

知道我在做什么错吗?为什么控制器中的FileContentResult可以正常工作,但是当我下载数据时却损坏了?

最佳答案

您将您的问题标记为MVC,因此我将尝试以MVC应用程序的身份进行回复。

REST对象似乎指示WEB.API,但也许我错了。 :)

如果您正在谈论FileContentResult,那么我建议您放入ContentType。

var contentType = System.Net.Mime.MediaTypeNames.Application.Pdf;


然后像这样加载FileContentResult:

var fcr = new FileContentResult(ms.ToArray(), contentType); //NOTE: Using a File Stream Result will not work.
fcr.FileDownloadName = FileName;


我看到您正在使用PDF生成,所以这是我用来生成PDF的FileContentResult的示例:

    public FileContentResult CreatePdfFileContentResult()
    {
        var contentType = System.Net.Mime.MediaTypeNames.Application.Pdf;
        using (MemoryStream ms = new MemoryStream())
        {
            // Create an instance of the document class which represents the PDF document itself.
            Document document = new Document(PageSize.A4, 25, 25, 30, 30);
            // Create an instance to the PDF file by creating an instance of the PDF
            // Writer class using the document and the filestrem in the constructor.
            PdfWriter writer = PdfWriter.GetInstance(document, ms);

            if (OnPdfMetaInformationAdd != null)
                OnPdfMetaInformationAdd(document, DataSource);

            // Open the document to enable you to write to the document
            document.Open();

            if (OnPdfContentAdd != null)
                OnPdfContentAdd(document, DataSource);
            else throw new NotImplementedException("OnPdfContentAdd event not defined");

            // Close the document
            document.Close();
            // Close the writer instance
            writer.Close();

            var fcr = new FileContentResult(ms.ToArray(), contentType); //NOTE: Using a File Stream Result will not work.
            fcr.FileDownloadName = FileName;
            return fcr;
        }
    }

关于c# - FileContentResult可以工作,但是不能下载到字节数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46103202/

10-13 01:33