Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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,
Posted
Updated 16-Apr-15 22:38pm
v2

1 solution

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:
C#
if (s != null)
{
    return new FileStreamResult(s, "application/pdf");
}

Or this:
C#
if (s != null)
{
    if (s.CanSeek)
        s.Seek(0, SeekOrigin.Begin);
    return new FileStreamResult(s, "application/pdf");
}
 
Share this answer
 
Comments
Raj.rcr 17-Apr-15 2:50am    
Hey thanks for your answer. It worked for the first time when I used your second method. But after some time, it stopped working. I cleared the cache and rebuild my application. still cannot.
Mario Z 17-Apr-15 3:25am    
Just to be clear, so you are able to export one PDF file, but when calling this GeneratePDFReport multiple times you do not get multiple PDF files, is this right?
Hmmm I cannot say with 100% certanty, but is it possible that your CreateReport does not generate the report?

For testing purposes why don't you try saving the files localy as well, before streaming them to a client's browser.
For example:

if (s != null)
{
s.Seek(0, SeekOrigin.Begin);

string randomLocalFileName = Guid.NewGuid().ToString() + ".pdf";
using (Stream localFile = File.Create(randomLocalFileName))
s.CopyTo(localFile);

s.Seek(0, SeekOrigin.Begin);

return new FileStreamResult(s, "application/pdf");
}

Are all the PDF files generated localy?
If they aren't then you may want to investigate that CreateReport because it is probably giving the s of null value.

Or you can just put a break point at if (s != null) and check if it's being executed at each GeneratePDFReport call.
Raj.rcr 17-Apr-15 4:09am    
I am tracing every time with the break point and can see the value "s" is not null. Only thing is, it is not going inside "if (s.CanSeek)".
Mario Z 17-Apr-15 4:48am    
Oh I see, so the CreateReport creates an unseek-able s stream, in that case to generate both local file and stream a file to a browser you would want to do that memory stream copying because you will write it twice, so:
if (s != null)
{
using(var ms = new MemoryStream())
{
s.CopyTo(ms);
ms.Seek(0, SeekOrigin.Begin);

string randomLocalFileName = Guid.NewGuid().ToString() + ".pdf";
using (Stream localFile = File.Create(randomLocalFileName))
ms.CopyTo(localFile);

ms.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(ms, "application/pdf");
}
}
Raj.rcr 17-Apr-15 4:17am    
And now, File.Create also not working in your above code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900