0

I have some problem. I'am try to save *.docx fail in sql server and show it in web-site. I can save my file in sql server using this code

 Byte[] bytes = File.ReadAllBytes(varFilePath);
        String file = Convert.ToBase64String(bytes);
        using (SqlConnection sql_connetion = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_connection"].ConnectionString))
        {
            sql_connetion.Open();
            using (var sqlWrite = new SqlCommand("INSERT INTO use_of_rule (ID, taj) Values('3', @File)", sql_connetion))
            {
                sqlWrite.Parameters.Add("@File", bytes);
                sqlWrite.ExecuteNonQuery();
                return "ok";
            }
        }

I want to display the content of the file in the web browser. How I can do it?

Cleptus
  • 3,446
  • 4
  • 28
  • 34
Abdumalik Nabiev
  • 311
  • 1
  • 3
  • 14
  • Sorry faile=file – Abdumalik Nabiev Jul 05 '18 at 10:52
  • You are using WAS? (Office Web App Server) – scegg Jul 05 '18 at 10:53
  • As you are saving it I guess its byte[] which is pretty much useless for you since you need the text.... In order to get the text you need to use "Microsoft.Office.Interop.Word" to open the file and read the contents. if you save your files in SQL as byte[] you will have to do some extra work when you are requesting to read the file and write the byte[] into a file before opening it... – S.Fragkos Jul 05 '18 at 10:53
  • "_and_ _show_ _it_", as in "Have a link a user can click and open the file" or "See the content of the file in the browser"? – Cleptus Jul 05 '18 at 10:59
  • What are you offer in this situation – Abdumalik Nabiev Jul 05 '18 at 10:59
  • See the content of the file in the browser – Abdumalik Nabiev Jul 05 '18 at 11:00
  • 1
    [How do I render a Word document (.doc, .docx) in the browser using JavaScript?](https://stackoverflow.com/questions/27957766/how-do-i-render-a-word-document-doc-docx-in-the-browser-using-javascript) – Alex K. Jul 05 '18 at 11:24
  • Possible duplicate of [How do I render a Word document (.doc, .docx) in the browser using JavaScript?](https://stackoverflow.com/questions/27957766/how-do-i-render-a-word-document-doc-docx-in-the-browser-using-javascript) – Cleptus Jul 05 '18 at 12:46

1 Answers1

0

If you are using Word from WAS (Office Web App Server), you need to do something like this:

1 Create a simple web site to read the data from sql. You could create a Generic Handler (ashx) in ASP.Net site, providing the plain word file data as response. Dont forget to set the content type as "application/vnd.openxmlformats-officedocument.wordprocessingml.document".

2 Use url encoding to encode the link created in step 1, including all parameters you may want to pass into it.

3 To launch Word, you need to nav it to http(s)://your.was.server.name/op/view.aspx?src=paste.encoded.url.here

scegg
  • 289
  • 2
  • 9