Click here to Skip to main content
14,828,290 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to convert a html file into word file,save that word file in a physical path in our project and after that how to get that particular path and insert into the database while button click???
(we can not use file upload control in mvc 5)

What I have tried:

private void download_character_certificate_word(string str_content_type, string memo_body, string school_id, string base_url)
            string strFileName= "Study_Certificate.doc";
            string strFilePath;
            string strFolder;
            StringBuilder str = new StringBuilder();

            Response.ContentType = str_content_type;
            Response.Charset = "";

            switch (str_content_type)
                case "application/":
                    Response.AddHeader("Content-Disposition", "attachment; filename=Study_Certificate.xls;");
                case "application/msword":
                    // Response.AddHeader("Content-Disposition", "attachment; filename=Study_Certificate.doc;");
                    Response.AddHeader("Content-Disposition", "attachment; filename="+ strFileName + "");
                default: break;

            StringBuilder sbResponseString = new StringBuilder();
            string wordHeader = "";
            //strFolder = sbResponseString.saveas(Server.MapPath("~/files/Download_study_certificate/"));
            //strFilePath = strFolder + strFileName;
            //System.IO.FileInfo file = new System.IO.FileInfo(strFilePath);
            Session["Study_certificate_word_document"] = sbResponseString.ToString();
            int length = sbResponseString.Length;
            Session["length"] = length;

            //string strPath = strFileName;
            ////string strTextToWrite = TextBox1.Text;
            //FileStream fStream = new FileStream("~/files/Download_study_certificate/"+strFileName+"", FileMode.Append);
            //StreamWriter sWriter = new StreamWriter(strPath);

Updated 12-Feb-21 5:41am

1 solution

Your code is running on the server. If you save the file in a specific path, it will be saved on the server. The user will not be able to access it.

If you want the user to open the file, you will need to send it via the response. You cannot control where, or even if, the user saves the file on their computer.

If you want the user to upload their changes back to the server, then you have to use an <input type="file">, and the user will have to select the updated file from their computer.

Saving and opening the file on the server might appear to work when you're debugging your site in Visual Studio. But that's only because, in that specific case, the server and the client are the same computer. As soon as you deploy your code to a real server, it will stop working. You'll either get an exception telling you that Office is not installed on the server, or the file will open on the server, where nobody will ever see it.

And if you were thinking of using Office Interop from your code, then you need to read this:
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

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