Solved

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

Posted on 2012-04-10
4
408 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
2 questions 10 25
Service Controller 1 24
MediaHelp 4 23
Example works but why do I need to click twice on button 4 43
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

825 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