File not opening in Treeview Treenode through navigateURL in Edge and Chrome

Gani_tpt 1,586 Reputation points
2022-06-16T11:03:41.51+00:00

I am trying to open the document using Treeview TreeNode NavigateURL in ASP.NET

The same will be working in Internet Explorer.

but, it will not working in Edge and chrome.

I tried many ways. but, no luck.

Also, Internet Explorer is deprecated and no more further support.

<asp:TreeView ID="TreeView1" runat="server"  Font-Names="Trebuchet MS" Font-Size="15px" ForeColor="blue" Font-Bold="true">  
                                        <Nodes>  
                                            <asp:TreeNode Text="Student List" Value="Student List">  
                                                <asp:TreeNode Text="Stud1" Value="Stud1" NavigateUrl="file://cals.calrow.intranet/proj/StudList/stud1.pdf"  Target="_blank">  
 </asp:TreeNode>  
                                                <asp:TreeNode Text="Stud2" Value="Stud2" NavigateUrl="file://cals.calrow.intranet/proj/StudList/stud2.pdf"   
  Target="_blank">  
 </asp:TreeNode>  
                                                ----  
                                                ----  
                                           </asp:TreeNode>  
                                         </Nodes>  
</TreeView>      

what could be the solution to do further....?

This is very urgent and trying to get correct solution from microsoft....pls. help

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,287 questions
{count} votes

Accepted answer
  1. Albert Kallal 4,806 Reputation points
    2022-06-17T20:47:34.577+00:00

    Ok, I think for the sake of comments and responses - I'm going to add anohter post here.
    (if I am supposed to edit my original post - then my apologes to this forum and group - just not sure which is perfered).

    As I noted, the real goal here is to take the existing treeview, and "convert" or "modify" or at the very least cook up a way to get the existing URL's with that "no more" supported file setting in the URL link.

    We can actually approach this problem like this:

    Is there a way to get that URL sent to the server, grab the URL, convert into a full plane jane server path name, and THEN use the information in my first post to stream (read + send) the file down to the browser.

    The old story comes down to this:

    How do you eat an elephant?
    Answer: one bite at a time.

    so, if we can get/grab/see/have that old URL and grab it?

    Then we can add code to the page - and not have to modify on a link by link bases (it would not make sense to add 100's of link buttons - too much work).

    So, assume this simple treeview and assume it has the old style "file" path names.

    Lets keep this simple as possible. So, we have this markup:

    212600-image.png

    Ok, so now what we do is add a wee bit of javascript to PROCESS the treeview, remove the URL, and convert that URL into a simple click event. That click event can then run server side code and dish out the file.

    So we add this JavaScript code to the page:

    212641-image.png

    So, what above does is converts the URL navagate into a on-click. The routine when clicked then simple shoves the URL (path) into a hidden control, and THEN clicks our button (you eventually hide that button with style="display","none").

    So, now our code behind is this:

        Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
      
            Dim strServerFile As String = hFileLink.Value  
            strServerFile = strServerFile.Replace("/", "\")  
      
            Call SendFile(strServerFile)  
      
        End Sub  
      
      
      
        Sub SendFile(strFile As String)  
      
            Dim strServerFile As String = strFile.Replace("file://", "")  
      
            Dim sFileNameOnly = Path.GetFileName(strServerFile)  
      
            Dim sMineType As String = MimeMapping.GetMimeMapping(sFileNameOnly)  
      
            If File.Exists(strServerFile) Then  
      
                Dim binFile As Byte() = File.ReadAllBytes(strServerFile)  
                Response.ContentType = sMineType  
                Response.AppendHeader("Content-Disposition", "inline; filename=" + sFileNameOnly)  
                Response.BinaryWrite(binFile)  
                Response.End()  
      
            End If  
      
        End Sub  
      
    

    So, what this means, is we don't have to modify each link in the treeview, but apply some code to the exising TV, wire up that server side event, and then we free to use the first bits and parts I posted in my other answer.

    The only thing that looks out of place here is the "file" path name(s) you have - they don't look like a "resolve" or path name to the exsting server - but the above approach, and ideas, and concepts should be albe to work here, and this means that we don't have to change the existing treeview much - but just apply some js code that changes it to what we need for this to work in modern browsers.

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada


2 additional answers

Sort by: Most helpful
  1. Albert Kallal 4,806 Reputation points
    2022-06-16T22:02:48.873+00:00

    This is not at all so bad.

    As noted, tossing in a full server path name into the browser and using "file://" is not going to work any more.

    However, all of your other code, including that treeview etc. can and should and will work just fine.

    the ONLY change you need?

    Don't pass the file:// to the end browser but have some code behind read the file, and "stream" it to the browser. While this sounds rather fancy, it actually is very easy to implement with your current system.

    What this means, is that you replace the hyper link file: with a button. (it call look like a hyper link, or you can even use a linkbutton with a click event.

    So, lets cook up a simple example. I will use a simple grid, but it could be any type of display.

    I will include both the older way (which will not work anymore), and the new way.

    So, say this grid of files:

    212226-image.png

    so, before, one had a hyper link, but now the code will look like this:
    (we replaced the hyper link with a link button)

    And now our on-click for this link button is this:

        Protected Sub cmdLink_Click(sender As Object, e As EventArgs)  
      
            Dim cmdLink As LinkButton = sender  
      
            Dim sFile As String = cmdLink.Attributes("File")  
            sFile = "~/" & Mid(sFile, 4)  
      
            sFile = Server.MapPath(sFile)  
            Dim sFileNameOnly = Path.GetFileName(sFile)  
            Dim sMineType As String = MimeMapping.GetMimeMapping(sFile)  
      
            If File.Exists(sFile) Then  
      
                Dim binFile As Byte() = File.ReadAllBytes(sFile)  
                Response.ContentType = sMineType  
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + sFileNameOnly)  
                Response.BinaryWrite(binFile)  
                Response.End()  
      
            End If  
      
        End Sub  
      
    

    You can ignore the first few lines of code. We simple take the file name - (without the file://) and then simple read the file from disk, and then send to the browser.

    The effect is similar. The file in most cases WILL NOT open, but will download.

    However, for text files, PDF, and even in some cases Excel, (at least csv), you can replace the above attachment with "inline", and the vast majority of all browsers will display the pdf.

    So, in effect, the place were you provided a "file://", you need to replace that with a button, and code that DIRECT reads the file from disk, and sends it directly to the browser.

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada


  2. AgaveJoe 26,141 Reputation points
    2022-06-18T11:28:06.013+00:00

    Again, in my opinion a generic handler is a more straight forward and standard approach. There's no UI, page life cycle etc.

    In Visual Studio create a Generic file handler. I named my generic handler "Files".

    212682-capture.png

    The code is rather simple but you might have to modify it to fit your application. This example assume the client sends the entire file path to the server in the URL's querystring.

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Web;  
      
    namespace WebFormsDb.Services  
    {  
        /// <summary>  
        /// Summary description for files  
        /// </summary>  
        public class Files : IHttpHandler  
        {  
      
            public void ProcessRequest(HttpContext context)  
            {  
                //Assumes the file path is passed in the query string  
                string filename = context.Request.QueryString["file"];  
      
                //Format the resposne  
                HttpResponse response = context.Response;  
                response.ContentType = "application/octet-stream";  
                response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);  
      
                //Transmit the file without buffering the file contents.  
                response.TransmitFile(filename);              
            }  
      
            public bool IsReusable  
            {  
                get  
                {  
                    return false;  
                }  
            }  
        }  
    }  
    

    To download the file using the treeview is a simple change to the update to the NavigationUrl property. Replace https://localhost:44361/services/files.ashx with your application URL.

    NavigateUrl="https://localhost:44361/services/files.ashx?file=cals.calrow.intranet/proj/StudList/stud2.pdf"