?
Solved

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

Posted on 2012-04-10
4
Medium Priority
?
446 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:
Meir Rivkin earned 750 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month16 days, 22 hours left to enroll

864 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