Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi team,

i want to create .doc files in server folder.

we have one server 101.20.123.11 in that server we have installed sql server.

my requierment is i have one page in that fill the fields and click on submit .whenever click on submit ,i need to create word file along with that fields data in our server folder and also i need to send mail as attachment this doc file through database.how to give the my server folder path for saving the doc file .

note:

in this page i am not using fileupload control.

please help.

What I have tried:

protected void btnSubmit_click(object sender,EventArgs e)
   {
       try
       {
           string FilePathSave = "";
           HttpContext.Current.Response.Clear();
           HttpContext.Current.Response.Charset = "";


           HttpContext.Current.Response.ContentType = "application/msword";

           string strFileName = string.Empty;
            strFileName = "ServiceAttachApprovalDocument-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc";
           HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);

           StringBuilder strHTMLContent = new StringBuilder();

           strHTMLContent.Append("<img src='" + @"D:\CSS Projects\Inventory_VSS_04-May-2017\ImagingTOOL\images\kodac1.png'>".ToString());

          // strHTMLContent.Append("< img src = '~/images/kodac.png' visible = 'true' runat = 'server' style = 'width: 75px; height: 41px' />".ToString());
           //strHTMLContent.Append("<br>".ToString());

           strHTMLContent.Append(" <h1 title='Heading' align='Center' style='font-family:verdana;font-size:100%;background:#C0C0C0'>SERVICE ATTACH APPROVAL FORM </h1>".ToString());
           strHTMLContent.Append("<br>".ToString());
           strHTMLContent.Append("<table width='100%' border='2'>".ToString());

           //strHTMLContent.Append("<tr>".ToString());
           //strHTMLContent.Append("<td style='width: 100px;background:#99CC00'>SERVICE ATTACH APPROVAL FORM</td>".ToString());
           //strHTMLContent.Append("</tr><br/>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td colspan='2'>Name of  End User </td>".ToString());
           strHTMLContent.Append("<td>"+txtEndusername.Text+"</td>".ToString());
           strHTMLContent.Append("<td>Name of Reseller</td>".ToString());
           strHTMLContent.Append("<td>" + txtResellerName.Text + "</td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td>Name of  Distributor </td>".ToString());
           strHTMLContent.Append("<td colspan='4'>" + txtDistributorName.Text + "</td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td>Cat No</td>".ToString());
           strHTMLContent.Append("<td>Model </td>".ToString());
           strHTMLContent.Append("<td> Total Quantity </td>".ToString());
           strHTMLContent.Append("<td> Total Period including Standard One year </td>".ToString());
           strHTMLContent.Append("<td> Name of nearest Competitor offering Service Attach </td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td>"+ddlproduct.SelectedItem.Text+"</td>".ToString());
           strHTMLContent.Append("<td>" + ddlproduct.SelectedItem.Text + "</td>".ToString());
           strHTMLContent.Append("<td> "+txtQuantity.Text+" </td>".ToString());
           strHTMLContent.Append("<td>"+txtperiodStdYear.Text+"</td>".ToString());
           strHTMLContent.Append("<td>"+txtCompetitorname.Text+"</td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td colspan='2'></td>".ToString());
           strHTMLContent.Append("<td>Distributor Price(INR) </ td>".ToString());
           strHTMLContent.Append("<td> Reseller  Price(INR)</ td>".ToString());
           strHTMLContent.Append("<td> End User Price(INR) </ td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td colspan='2'>Unit Product Sales Price Without Discount</td>".ToString());
           strHTMLContent.Append("<td>"+txtUPSWOutdcntDistributorprice.Text+"</ td>".ToString());
           strHTMLContent.Append("<td> " + txtUPSWOutdcntResellerPrice.Text + "</ td>".ToString());
           strHTMLContent.Append("<td>" + txtUPSWOutdcntEndUserPrice.Text + "</ td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td colspan='2'>Unit Product Sales Price With Discount</td>".ToString());
           strHTMLContent.Append("<td>" + txtUPSWithdcntDistributorprice.Text + "</ td>".ToString());
           strHTMLContent.Append("<td> " + txtUPSWithdcntResellerPrice.Text + "</ td>".ToString());
           strHTMLContent.Append("<td>" + txtUPSWithdcntEndUserPrice.Text + "</ td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td colspan='2'>Unit Service Attach Without Discount</td>".ToString());
           strHTMLContent.Append("<td>" + txtUSAWOutdcntDistributorprice.Text + "</ td>".ToString());
           strHTMLContent.Append("<td> " + txtUSAWOutdcntResellerPrice.Text + "</ td>".ToString());
           strHTMLContent.Append("<td>" + txtUSAWOutdcntEndUserPrice.Text + "</ td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td colspan='2'>Service Attach With Discount</td>".ToString());
           strHTMLContent.Append("<td>" + txtUSAWithdcntDistributorprice.Text + "</ td>".ToString());
           strHTMLContent.Append("<td> " + txtUSAWithdcntResellerPrice.Text + "</ td>".ToString());
           strHTMLContent.Append("<td>" + txtUSAWithdcntEndUserPrice.Text + "</ td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("</table>".ToString());
           strHTMLContent.Append("<br>".ToString());
           DateTime WDate = Convert.ToDateTime(System.DateTime.Now.ToShortDateString());
           string WarrentyDate = WDate.ToString("dd/MM/yyyy");
           strHTMLContent.Append(" Above prices valid for Warranty until (dd/mm/yyyy) :"+ WarrentyDate);
           strHTMLContent.Append("<br>".ToString());
           strHTMLContent.Append("<br>".ToString());
           strHTMLContent.Append("<table border='2' width='100%'>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td>Name</td>".ToString());
           strHTMLContent.Append("<td>(Sales Manager)</ td>".ToString());
           strHTMLContent.Append("<td>(Service Manager)</ td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td>Signature</td>".ToString());
           strHTMLContent.Append("<td></td>".ToString());
           strHTMLContent.Append("<td></td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td>Date</td>".ToString());
           strHTMLContent.Append("<td></td>".ToString());
           strHTMLContent.Append("<td></td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("</table>".ToString());

           strHTMLContent.Append("<br><br>".ToString());
           strHTMLContent.Append("<p align='Center'> Note : This is dynamically generated word document </p>".ToString());


           //StreamWriter sw = File.CreateText(MapPath("myFile.txt"));
           //sw.WriteLine( txtFile.Text );
           //sw.Close();

           // Convert to string
           string strFileData = strHTMLContent.ToString();
           string strFolderValue = @"\101.20.123.11\c:\";//this is server path
          // string strFolderValue = @"D:\CSS Projects\Inventory_VSS_04-May-2017\ImagingTOOL\Word\"; //local path..
           //System.IO.
           DirectoryInfo Folder = new DirectoryInfo(strFolderValue);
           //objprop.P_SAFilename = strFileName;
           //objprop.P_SAFilepath = strFolderValue.ToString() + strFileName.ToString();
           string SAFilename = strFileName;
           string SAFilePath= strFolderValue.ToString() + strFileName.ToString();
           if (Folder.Exists)
           {
               if (strFileData != string.Empty)
               {
                   // build the path
                   FilePathSave = Folder.ToString() + strFileName;

                   // if file exist delete and then save again
                   // else save file
                   File.WriteAllText(FilePathSave, strFileData); // is supposed to overwrite the file if it is there but doesn't

               }
               else
               {
                   Response.Write("(Nothing to save)");
               }

               // If we can't find the folder
           }
           else
           {
               Response.Write("Folder [" + strFolderValue + "not found]");
           }
            BindProperties();
         int i=  objpo.InsertServiceAttachApproval(objprop, SAFilename, SAFilePath);
         if(i>0)
           {
               lblMsg.Text = "Inserted succesfully.";
               objpo.MAIL_FOR_ServiceAttchApprovalForDocFile();
               Clear();
           }

       }
       catch (Exception ex)
       {
           throw;
       }
   }
Posted
Updated 31-May-17 8:43am

1 solution

On the server, these things do not work — unless your hosting environment provides you with these paths,
strHTMLContent.Append("<img src='" + @"D:\CSS Projects\Inventory_VSS_04-May-2017\ImagingTOOL\images\kodac1.png'>".ToString());

There, you would need to do something like this,
C#
strHTMLContent.Append("<img src='" + Server.MapPath("~/ImagingTOOL") + 
                    @"images\kodac1.png'>".ToString());

This would, check for the ImagingTOOL path in the root of your web app's file system. So, what needs to be done is to check where your files would reside on the server and then using Server.MapPath() fetch the physical path for them. ASP.NET would take care of mapping the paths.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900