?
Solved

problem creating pdf document using filestream and storing in DB

Posted on 2014-08-27
1
Medium Priority
?
188 Views
Last Modified: 2014-09-11
I have an application that is creates a PDF and then stores it in a database. On my local machine everything works great, but it fails with the following error when on the production server. The file stream  Line 66 is.

using (FileStream fs = File.Create(@"Eval" + DaRec + ".pdf"))

Open in new window


My application is a .net 4.5

The error Message

System.UnauthorizedAccessException: Access to the path 'C:\Windows\SysWOW64\inetsrv\Eval1200.pdf' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) at System.IO.File.Create(String path) at GeneratePDF.Page_Load(Object sender, EventArgs e) in c:\Projects\appcreate\GeneratePDF.aspx.cs:line 66

Although this error talked about access denied. My server person indicates the error may be towards a machine key issue based on info from the server error logs. He sent me this address based on that error.  

http://blogs.msdn.com/b/tess/archive/2009/09/16/forms-authentication-fails-after-installing-iis-7-5.aspx

Here is my machine key currently in the web config

 <machineKey validationKey="1640985E8BF5DF25E040ED58ADB257AA8BE739C5B789E0FDE1BA08E11D8A1C13AFC0211C5CFC76306F4A860D32B1856B67A868D11C0B0280CB52B6159F5F08DF" decryptionKey="F52FC385309C58D876FCDBB5F0D8DEA581E645CA5513973B0241E6668C678355" validation="SHA1" decryption="AES" compatibilityMode="Framework45" />

Open in new window


Current code

using (MemoryStream myMemoryStream = new MemoryStream())
                {
                    try
                    {

                        Document myDocument = new Document(PageSize.A1, 10f, 10f, 10f, 0f);
                        PdfWriter myPDFWriter = PdfWriter.GetInstance(myDocument, myMemoryStream);
                        myDocument.Open();
                        WebClient wc = new WebClient();
                        string htmlText = wc.DownloadString("http://Prod40202/appcreate/GeneratePDF/CreatePDF.aspx?pid=" + DaRec);
                        Response.Write(htmlText.Replace("\r", "").Replace("\n", "").Replace("  ", ""));

                        List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), null);

                        for (int k = 0; k < htmlarraylist.Count; k++)
                        {

                            myDocument.Add((IElement)htmlarraylist[k]);
                        }
                        myDocument.Close();
                        byte[] content = myMemoryStream.ToArray();
                        //    document.Close();
                        using (FileStream fs = File.Create(@"Eval" + DaRec + ".pdf"))
                        {

                            fs.Write(content, 0, (int)content.Length);
                            var p5 = ctx1.tblAttachments.SingleOrDefault(u => u.AttachmentID == RecID);
                            if (p5 != null)
                            {
                                p5.DocName = "PayEval" + DaRec + ".pdf";
                                p5.DocType = "pdf";
                                p5.DocByteSize = Convert.ToInt32(fs.Length);
                                p5.DocBinary = content;
                                p5.DateCreated = DateTime.Now;
                                ctx1.SaveChanges();
                                Response.Redirect("OpenAttachment.aspx?pid=" + RecID);

                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.ToString());
                    }
                }

Open in new window

0
Comment
Question by:NickMalloy
1 Comment
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 2000 total points
ID: 40289372
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

850 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