Solved

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

Posted on 2012-04-10
4
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

719 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