0

I have a web application in c#,that creates and save a document in local machine folder. The works fine when running from my machine - however as soon as I deploy the app to my web server I get this error.

This is not a valid file name. Try one or more of the following: * Check the path to make sure it was typed correctly. * Select a file from the list of files and folders.

Below is my source code

            //Create an instance for word app  
            Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();

            //Set animation status for word application  
            winword.ShowAnimation = false;

            //Set status for word application is to be visible or not.  
            winword.Visible = false;

            //Create a missing variable for missing value  
            object missing = System.Reflection.Missing.Value;

            //Create a new document  
            Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);

            //Add header into the document  
            //foreach (Microsoft.Office.Interop.Word.Section section in document.Sections)
            //{
            //    //Get the header range and add the header details.  
            //    Microsoft.Office.Interop.Word.Range headerRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
            //    headerRange.Fields.Add(headerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage);
            //    headerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
            //    headerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
            //    headerRange.Font.Size = 10;
            //    headerRange.Text = "Header text goes here";
            //}

            //Add the footers into the document  
            //foreach (Microsoft.Office.Interop.Word.Section wordSection in document.Sections)
            //{
            //    //Get the footer range and add the footer details.  
            //    Microsoft.Office.Interop.Word.Range footerRange = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
            //    footerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkRed;
            //    footerRange.Font.Size = 10;
            //    footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
            //    footerRange.Text = "Footer text goes here";
            //}

            //adding text to document  
            document.Content.SetRange(0, 0);
            document.Content.Text = "This is test document " + Environment.NewLine;

            //Add paragraph with Heading 1 style  
            Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
            object styleHeading1 = "Heading 1";
            para1.Range.set_Style(ref styleHeading1);
            para1.Range.Text = "Para 1 text";
            para1.Range.InsertParagraphAfter();

            //Add paragraph with Heading 2 style  
            Microsoft.Office.Interop.Word.Paragraph para2 = document.Content.Paragraphs.Add(ref missing);
            object styleHeading2 = "Heading 2";
            para2.Range.set_Style(ref styleHeading2);
            para2.Range.Text = "Para 2 text";
            para2.Range.InsertParagraphAfter();

            //Create a 5X5 table and insert some dummy record  
            Microsoft.Office.Interop.Word.Table firstTable = document.Tables.Add(para1.Range, 5, 5, ref missing, ref missing);

            firstTable.Borders.Enable = 1;
            foreach (Row row in firstTable.Rows)
            {
                foreach (Cell cell in row.Cells)
                {
                    //Header row  
                    if (cell.RowIndex == 1)
                    {
                        cell.Range.Text = "Column " + cell.ColumnIndex.ToString();
                        cell.Range.Font.Bold = 1;
                        //other format properties goes here  
                        cell.Range.Font.Name = "verdana";
                        cell.Range.Font.Size = 10;
                        //cell.Range.Font.ColorIndex = WdColorIndex.wdGray25;                              
                        cell.Shading.BackgroundPatternColor = WdColor.wdColorGray25;
                        //Center alignment for the Header cells  
                        cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                        cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

                    }
                    //Data row  
                    else
                    {
                        cell.Range.Text = (cell.RowIndex - 2 + cell.ColumnIndex).ToString();
                    }
                }
            }

            //Save the document  
            object filename = @"c:\Data\TestSignature.doc";

            document.SaveAs(ref filename);
            document.Close(ref missing, ref missing, ref missing);
            document = null;
            winword.Quit(ref missing, ref missing, ref missing);
            winword = null;
            Label7.Text = "Document created successfully !";
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43

2 Answers2

0

You can not write anywhere on server - for example you can not use this directory @"c:\Data\TestSignature.doc"; Maybe if you add permissions, but better not.

Use this directory inside your asp.net folder to write data that you do not want to access public.

/App_Data/

Or make some other directory in your web site folder and saved them there.

You can use the HttpRuntime.AppDomainAppPath to get the path of your site, and add the extra directory.

In the folder you going to write you must give write permissions of your pool identity : How to set correct file permissions for ASP.NET on IIS

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • Thank you for response - let me try and make this more clearer. the @"c:\Data is the local/client user path.the document is downloaded on the app and it should be save onto the C-drive of the user, and not on the server.the assumption is that the Data folder on the user machine is accessible by the server where is app is deployed – Sphamandla Nyathikazi Feb 19 '20 at 15:37
0

Apologies for not being clear enough on my question. The document that is being created by the must,must be saved on the user/client computer. The assumption is that all users will have C drive and a folder called Data.the folder will have necessary permissions to save the file.

  • this is totally different - use a handler to send this data to the user as download file, or after you make it, also give a link so the user can download it - You can not automatically save it to your user where you want - you can only offer it as download file – Aristos Feb 23 '20 at 15:34