1

I need to develop a web application to open an ODT file when I click an image button in the browser. Below is the code that I used to open the file on a button click. It works for a Word document but it is not working for an Open Office document. How can I do this?

<asp:ImageButton id="imagebutton1" runat="server"
    AlternateText="ImageButton 1"
    Image
    ImageUrl="images/pict.jpg"
    OnClick="imagebutton1_Click"/>

protected void imagebutton1_Click(object sender, ImageClickEventArgs e)
{
    Response.Clear();
    Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    Response.AddHeader("Content-disposition", "inline; filename=TestDoc.docx");
    String path = Server.MapPath("TestDoc.docx");
    Response.WriteFile(path);
    Response.End(); 
}
kayess
  • 3,384
  • 9
  • 28
  • 45

1 Answers1

0

Looking at the OpenOffice Reference for File MIME Types, it looks as if the MIME type you're sending back when you set your ContentType property is wrong.

In their reference they list the ODT MIME Type as application/vnd.oasis.opendocument.text

I would suggest setting this MIME Type in your Response Object.

Source: http://www.openoffice.org/framework/documentation/mimetypes/mimetypes.html

Dan Gardner
  • 1,069
  • 2
  • 13
  • 29
  • Hi Dan Gardner thank you for your response but still i am unable to open the odt document it is still downloading but not getting opened on button click can you please help me out –  Dec 16 '16 at 16:36
  • Hi, sorry I think I might have misunderstood the original question. Are you saying that when you click you image you're file downloads but is not automatically opened by another desktop application (e.g. OpenOffice)? – Dan Gardner Dec 16 '16 at 17:01
  • yes exactly on clicking the image i should open the odt document which is in my application folder but it is getting downloaded –  Dec 16 '16 at 17:13
  • I'm fairly certain that this is not possible through web applications. You can see this would be a potential security concern form the browser. I believe that the reason this works for your Word Document is that certain browsers can associate actions with certain _safe_ filetypes the user has configured. Which browser are you using? – Dan Gardner Dec 16 '16 at 17:15
  • even it it not opening the word document also while downloading we will be able to view the currently being downloaded file know if we give right click on it and choose always open files of this type then they are getting opened but i cant do that it should be done programatically to open the files but pdf files are getting opened and this is the link http://www.c-sharpcorner.com/UploadFile/5089e0/how-to-open-pdf-files-in-web-brower-using-Asp-Net/ –  Dec 16 '16 at 17:30
  • i am unable to know why pdf doc is getting opened but not word doc and open office spread sheet document it is do or die for me can you please help me out –  Dec 16 '16 at 17:35
  • Please dan gardner please help me out it is do or die for me –  Dec 16 '16 at 17:58
  • I'm unsure how you can achieve this, I believe that the example you showed me renders a PDF as a webpage. It would be possible for you to achieve something similar in two ways. The first and simples would be to look for a third party plugin to display files with a third party library, Group Doc is an example of a premium library http://www.groupdocs.com/products/viewer/net. The second would be to parse and display the document yourself. This would be a reliably task if you're only looking to display simple text, but if you require formatting markup this could easily become a huge task. – Dan Gardner Dec 17 '16 at 00:21
  • I would consider asking yourself why you need this functionally, and if there is another way this can be achieved. If you are dead set on displaying in a browser one final idea I would suggest would be looking into converting (again probably using an extra all library) from ODF to PDF, and then displaying this? – Dan Gardner Dec 17 '16 at 00:23