locked
File not Opening Directly RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have below code but Doc/XLs files are not opening directly. First gets downloaded and then i have to open file.

     <asp:GridView ID="gvw"  runat="server" 
                                            CssClass="table table-bordered table-striped" AutoGenerateColumns="false"  
                                             ClientIDMode="Static"
                                            EmptyDataText="No Records Found!" EmptyDataRowStyle-ForeColor="Red" EmptyDataRowStyle-CssClass ="gvEmpty" >
                                            <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#428bca"></HeaderStyle>
                                            <Columns>
                                                
                                                <asp:TemplateField HeaderText="Link">
                                                    <ItemTemplate>
                                                        <a href='<%#Eval("Link0") %>'><%#Eval("Name") %></a>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                            </Columns>
                                            </asp:GridView>

    Thanks

    Friday, December 21, 2018 6:53 AM

All replies

  • User-2054057000 posted

    Change the ItemTemplate to 

    <ItemTemplate>
      <a href='ms-word:ofv|u|<%#Eval("Link0") %>'><%#Eval("Name") %></a> 
    </ItemTemplate>

    Full information is given in this article https://docs.microsoft.com/en-us/office/client-developer/office-uri-schemes

    Friday, December 21, 2018 9:55 AM
  • User36583972 posted

    Hi JagjitSingh,

    I have below code but Doc/XLs files are not opening directly. First gets downloaded and then i have to open file.

    You can also try the following suggestion: When the office software is installed on your computer, it will open the file automatically.

     <asp:TemplateField HeaderText="Link">
                            <ItemTemplate>
                               <asp:HiddenField runat="server" ID="hidelink" Value='<%#Eval("Link0")%>' />
                                 <asp:LinkButton ID="LinkButton1" runat="server"   OnClick="LinkButton1_Click"><%#Eval("NameLink") %></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
          protected void LinkButton1_Click(object sender, EventArgs e)
            {
                LinkButton openLink = (LinkButton)sender;
                GridViewRow row = (GridViewRow)openLink.NamingContainer;
                string firstCellText = row.Cells[0].Text; // here we are
                HiddenField lblValue = (HiddenField)row.FindControl("hidelink");
    
                var path = Server.MapPath(lblValue.Value);
                Response.ContentType = "txt/plain";
                Response.AddHeader("Content-Disposition", "attachment;filename=newworld.docx");
                Response.TransmitFile(path);
                Response.End();
            }
    

    Best Regards,

    Yong Lu

    Monday, December 24, 2018 7:46 AM
  • User-1499457942 posted

    Hi

      It can be excel file also. Notepad or txt files are opening directly

    Thanks

    Monday, December 24, 2018 3:52 PM
  • User36583972 posted

    Hi JagjitSingh

    It can be excel file also. Notepad or txt files are opening directly


    As far as I know, currently, there is no such way to open files directly. The file is stored on the server, you need to request the file, and then your machine can open with the corresponding program according to the file type.

    Browsers are not supposed to support this proprietary format. Even if you want your solution to be based on the MS Office format.

    May be you can define some mapping between some class of documents and HTML, represent the document as HTML, In this way, you can directly tell the class of the file to display through the web page.

    Then, you can also provide the editing capabilities through editable HTML. You can convert it back to Office format using the mapping your defined.

    You can try to use Open XML SDK to manipulate Office documents.

    You could visit Word processing (Open XML SDK) to see several samples.

    Besides, sometimes it is a good choice to refer to some three-party libraries that operate office files.

    Best Regards,

    Yong Lu

    Tuesday, December 25, 2018 6:20 AM