[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.0

ASP.NET 2.0 - Create PDF on Web server, stream to client, and deletre file

Asked by tigris_7 in Programming for ASP.NET, Microsoft IIS Web Server, Miscellaneous Web Development

Tags: IE, FF, Chrome

I can't believe I am still having trouble with stuff like this, but I am.

I am developing an Asp.Net 2.0 web app (on HTTPS) where the flow is as follows:

Page one gathers some pertinent info for creating the appropriate PDF file, redirects to Page 2

Page 2 retrieves info from session and PDF file is created successfully on server via iTextSharp, a couple more session vars are created, and  then redirects back to Page 1.

Page 1 attempts to stream file to client via the attached code snippet. The result is a blank PDF, whose size matches the size of the file on the server, and the opened file always has a [1] or [2] and so on appended to the name, as if it is a copy of an already existing file.It seems to stream the file to the IE Temp folder first but for some reason the file is blank, but as expected on the server.

A file path example is path = @"C:\sites\pfgboss\files\" + filename;

This code works for me in another page. I have experimented with the Response.AddHeader section to no avail.

All the code works on my non-https Windows XP development machine

Please help...I am stuck. Any suggestions are welcome, even trying another approach.




1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
/// <summary>
    /// Helper function for 'FindAndStreamPdfFromServer'...
    /// Streams an eStatement Pdf from server to client machine
    /// </summary>
    /// <param name="url"></param>
    private void StreamPdfFromServer(string url, string dl_filename)
    {
        FileStream fs = null;
        byte[] bytBytes = null;
        bool success = false;
        //check if file exist
        if (File.Exists(url))
        {
            try
            {
                //if file exists open file to stream and force download on the client
                fs = File.Open(url, FileMode.Open);
                bytBytes = new byte[fs.Length];
                fs.Read(bytBytes, 0, Int32.Parse(fs.Length.ToString()));
                fs.Close();
                success = true;
            }
            catch (Exception ex)
            {
                ErrorLog.logMsg("Error : " + ex.ToString());
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
            try
            {
                if (success && bytBytes != null && url.EndsWith("pdf"))
                {                                       
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.ClearContent();
                    Response.Buffer = true;
                    Response.AddHeader("Content-Length", bytBytes.Length.ToString());
                    Response.AddHeader("Content-disposition", "attachment; filename=" + dl_filename);
                    Response.AddHeader("Expires", "0");
                    Response.AddHeader("Pragma", "cache");
                    Response.AddHeader("Cache-Control", "private");
                    Response.ContentType = "application/pdf";
                    Response.BinaryWrite(bytBytes); 
                }
                else if (success && bytBytes != null && url.EndsWith("txt"))
                {
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.ClearContent();
                    Response.AddHeader("Content-disposition", "attachment; filename=" + dl_filename);
                    Response.ContentType = "application/txt";
                    Response.BinaryWrite(bytBytes);
                }
                else
                {
                    ErrorLog.logMsg("Error : FindFileAndDisplay(string url) - File:" + url + "doesn't exist");
                }
            }
            catch (Exception ex)
            {
                string temp = ex.Message;
            }
            finally
            {
                //if(File.Exists(url))
                //    File.Delete(url);
            }
        }
    }
[+][-]09/16/08 01:05 AM, ID: 22485970Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/16/08 09:58 AM, ID: 22490588Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Programming for ASP.NET, Microsoft IIS Web Server, Miscellaneous Web Development
Tags: IE, FF, Chrome
Sign Up Now!
Solution Provided By: tigris_7
Participating Experts: 2
Solution Grade: A
 
[+][-]09/16/08 10:29 PM, ID: 22495728Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89 / EE_QW_2_20070628