Solved

To save dynamically generated pdf to web server automatically without prompting user

Posted on 2012-04-10
4
404 Views
Last Modified: 2012-04-20
Hi Experts,

I am generating rdlc report in asp.net (visual studio 2010). Currently, using the following code, I am able to export the report to pdf format on a button click event. Response.Flush()  in the end is prompting user to download it at the desired location.

What I want is,
1) Instead of generating a pdf on a button click, I want to export my report automatically  to pdf after the report gets generated on a page load event
2) That pdf report should get saved automatically at the specified location on the web server instead of prompting user to download it on their PC.

Is this achievable?



 protected void CreatePDF()
        {
           
            // Variables
            Warning[] warnings;
            string[] streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;
            string fileName="Quote"+hdnRegistrationNo.Value+".pdf";
            string savelocation = Server.MapPath("Registrations")+ "\\" + fileName;
           
            // Setup the report viewer object and get the array of bytes
         
            byte[] bytes = ReportViewer1.LocalReport.Render("PDF", null, out mimeType, out     encoding, out extension, out streamIds, out warnings);

            Response.Buffer = true;
            Response.Clear();
            Response.ContentType = mimeType;
            Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
            Response.Flush();
           
        }
0
Comment
Question by:Tina_Bhole
  • 3
4 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 250 total points
ID: 37831256
after calling LocalReport.Render, use the following code to save it on the server:

-> fileName: the pdf file path on the server

using (FileStream stream = File.OpenWrite(fileName))
  {
    stream.Write(bytes , 0, bytes.Length);
  }

Open in new window

0
 

Author Comment

by:Tina_Bhole
ID: 37835943
Thanks for your reply sedgwick. Is there any way to eliminate clicking of the button to call CreatePDF() function. Can it be called automatically after the report loads?
0
 

Assisted Solution

by:Tina_Bhole
Tina_Bhole earned 0 total points
ID: 37849435
I managed to achieve what I wanted by calling CreatePDF() in Form_Load event as follows:


 if (IsPostBack)
            {
                CreatePDF();
            }


After the report loads it calls postback event and then the pdf gets generated automatically.
0
 

Author Closing Comment

by:Tina_Bhole
ID: 37869827
Sedwick answered one out of 2 questions I asked. I found answer to the second question on my own.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
LINQ question 7 22
Default parameter problem in C# 3 27
c# if statement weird reaction 3 29
Adjust the position 3 13
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now