locked
illegal charcters in path when viewer files in browser RRS feed

  • Question

  • User-289010069 posted

    I'm trying to display files in a browser window when a gridview linkbutton is clicked on RowCommand.  Whenever I try to display a file, I receive an illegal character in path error message.  I'm sure it has something to do with the characters in the sample file path below.   Can someone offer any advice on how to resolve the issue?

    ex. file path format - \\directory1\directory2\232-3434-34354-4334.TestFile.txt

    Gridview

       <asp:GridView ID="DocGrid"   runat="server" CssClass="table table-striped table-hover   "
                                        AutoGenerateColumns="False" >
                                        <Columns>
                                            <asp:TemplateField>
                                                    <ItemTemplate>
                                                        <asp:LinkButton id="lbView" runat="server" CommandName="ViewDoc"  Text="View Document" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>">
                                                        
                                                        </asp:LinkButton>
                                                    </ItemTemplate> 
                                            </asp:TemplateField> 
                                            <asp:BoundField DataField="doc_id" HeaderText="Doc ID" />
                                            <asp:BoundField DataField="Description" HeaderText="Description" />
                                         
                                        </Columns>
                                    </asp:GridView>
    
    
    
    
     Private Sub DocGrid_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles DocGrid.RowCommand
    
            Dim dsDocs As New DataSet
            Dim drDoc As DataRow = Nothing
    
            If e.CommandName = "ViewDoc" Then
            
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)
                Dim row As GridViewRow = DocGrid.Rows(index)
    
                If row IsNot Nothing Then
                    
                    Try
                       
                        If  dsDocs.Tables(0).Rows.Count > 0 Then
                            drDoc = dsDocs.Tables(0).Rows(0)
                            Dim lbViewDoc As LinkButton = row.FindControl("lbView")
                            Dim path As String = drDoc("FilePath")
                              Dim FileScript As String = "<script language='javascript'>window.open('" + path + "','TestFile');</script>"
      Page.RegisterStartupScript("open window", FileScript) End If Catch ex As Exception End Try End If End If End Sub

    Friday, May 29, 2020 3:54 PM

All replies

  • User475983607 posted

    The design will not work because the link is trying to access local resources which violates browser security.

    Friday, May 29, 2020 4:09 PM
  • User-289010069 posted

    Is there any way to view a file in a pop up window from a linkbutton in the gridview row command event?

    Friday, May 29, 2020 4:28 PM
  • User475983607 posted

    Is there any way to view a file in a pop up window from a linkbutton in the gridview row command event?

    Use the URL of the text file.  If the file is outside the hosted application then write an HTTP Handler to return the file.  The URL is the HTTP Handler where the file is a parameter.

    https://www.dotnetperls.com/ashx

    https://stackoverflow.com/questions/12087040/file-download-by-calling-ashx-page

    http://www.intstrings.com/ramivemula/articles/how-to-download-files-from-server-to-client-using-a-generic-handler/

    Friday, May 29, 2020 5:12 PM
  • User288213138 posted

    Hi HelloWrold18,

    HelloWorld18

    Is there any way to view a file in a pop up window from a linkbutton in the gridview row command event?

    Due to security issues, the browser cannot open local file, if you want to open the local file in asp.net, you can try below code:

    <asp:LinkButton ID="lnkView" runat="server" Text="View File" OnClick="View"></asp:LinkButton>
    <hr />
    <asp:Literal ID="ltEmbed" runat="server" />
    protected void View(object sender, EventArgs e)
    {
        string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"500px\" height=\"300px\">";
        embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";
        embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
        embed += "</object>";
        ltEmbed.Text = string.Format(embed, ResolveUrl("~/Files/test.pdf"));  
    }

    More information about how to open a file you can refer to this link:

    https://www.aspsnippets.com/Articles/Open-Show-PDF-File-from-Server-in-ASPNet.aspx

    Best regards,

    Sam

    Monday, June 1, 2020 6:52 AM
  • User-289010069 posted

    Thanks for the example but I need a way to open different types of files(excel, word, pdf, txt, etc..) in the browser from the Row Command button click event.  The example is for pdf files. 

    Monday, June 1, 2020 11:52 AM
  • User475983607 posted

    HelloWorld18

    Thanks for the example but I need a way to open different types of files(excel, word, pdf, txt, etc..) in the browser from the Row Command button click event.  The example is for pdf files. 

    What is stopping you from updating the code to suite your needs?  Keep in mind, opening a document in a browser window requires browser support.  PDF, Word, and Excel require a browser plugin.   Most browser support PDF but not Word and Excel.

    Monday, June 1, 2020 12:04 PM
  • User288213138 posted

    Hi HelloWrold18,

    Thanks for the example but I need a way to open different types of files(excel, word, pdf, txt, etc..) in the browser from the Row Command button click event.  The example is for pdf files. 

    You can try below code, for some browser reasons, I suggest you download the file and then go to view.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText="No files uploaded">
                    <Columns>
                        <asp:BoundField DataField="Text" HeaderText="FileName" />
                        <asp:TemplateField HeaderText="View">
                            <ItemTemplate>
                                <asp:LinkButton ID="lbView" Text="View" CommandArgument='<%# Eval("Value") %>' runat="server" OnClick="ViewFile"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>     
                    </Columns>
                </asp:GridView>
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Not IsPostBack Then
                Dim filePaths As String() = Directory.GetFiles(Server.MapPath("~/Files/"))
                Dim files As List(Of ListItem) = New List(Of ListItem)()
    
                For Each filePath As String In filePaths
                    files.Add(New ListItem(Path.GetFileName(filePath), filePath))
                Next
    
                GridView1.DataSource = files
                GridView1.DataBind()
            End If
        End Sub
    
        Protected Sub ViewFile(ByVal sender As Object, ByVal e As EventArgs)
            Dim filePath As String = (TryCast(sender, LinkButton)).CommandArgument
            Response.ContentType = ContentType
            Response.AppendHeader("Content-Disposition", "attachment; filename=" & Path.GetFileName(filePath))
            Response.WriteFile(filePath)
            Response.[End]()
        End Sub

    Best regards,

    Sam

    Tuesday, June 2, 2020 3:29 AM