从MVC中的内存流下载pdf [英] download pdf from memory stream in MVC

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

问题描述

亲爱的所有人,



我无法将内存流下载到pdf文件中。当我跟踪程序时,我得到了数据并成功完成但它没有下载pdf。



以下是我的代码:



Dear All,

I am unable to download the memory stream into pdf file. When I trace the program, I get the data and completes successfully but it is not downloading pdf.

Below is my code :

[HttpGet]
public ActionResult GeneratePDFReport()
        {
            EVance.NotifierSaas.Web.Source.Infrastructure.ReportGenerationConnection objReport = new Source.Infrastructure.ReportGenerationConnection();
            using (ReportManagement.ReportManagementClient report = new ReportManagement.ReportManagementClient())
            {
                List<int> signatureImageList = new List<int>();
                List<string> signatureNameList = new List<string>();
                Stream s = null;
                string strDateTime = DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss tt");
                string errorMessage = report.CreateReport(objReport.getAccountConnection(CurrentUser.UserName),
                                                            new DateTime(2015, 4, 15),
                                                            2, // Fault Handle Data 
                                                            -300,
                                                            int.Parse("0"),
                                                            0,
                                                            12,
                                                            signatureImageList.ToArray(),
                                                            signatureNameList.ToArray(),
                                                            DateTime.UtcNow,
                                                            CurrentUser.UserName,
                                                            out s);


                if (s != null)
                {                   
                    // write out the report
                    MemoryStream ms = new MemoryStream();
                    s.CopyTo(ms);
                    TextWriter textWriter = new StreamWriter(ms);
                    textWriter.WriteLine("Something");
                    textWriter.Flush(); // added this line
                    byte[] bytesInStream = ms.ToArray(); // simpler way of converting to array
                    //ms.Close();

                    ms.Write(bytesInStream, 0, bytesInStream.Length);
                    ms.Position = 0;
                    
                    return new FileStreamResult(ms, "application/pdf");
                }
            }

            return RedirectToAction("Reports", "App");
        }





我们的帮助将不胜感激。



问候,



Your help would be appreciated.

Regards,

推荐答案

为什么要用 ms textWriter <完成所有工作? / code>和 bytesInStream ,你想用它做什么?

如果你想转换你的 s 流到内存流然后只需使用 s.CopyTo(ms); ms.Position = 0;



但在此之前,我不确定结果类型 out s CreateReport 中,但为什么不尝试以下内容:

Why are you doing all that work with ms, textWriter and bytesInStream, what did you want to accomplish with that?
If you want to convert your s stream to memory stream then just use s.CopyTo(ms); and ms.Position = 0;.

But before that, I'm not exactly sure what is the resulting type of out s in CreateReport, but nevertheless why don't you just try the following:
if (s != null)
{
    return new FileStreamResult(s, "application/pdf");
}



或者:


Or this:

if (s != null)
{
    if (s.CanSeek)
        s.Seek(0, SeekOrigin.Begin);
    return new FileStreamResult(s, "application/pdf");
}


这篇关于从MVC中的内存流下载pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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