href Link not working in ASP.NET PAGE

BeUnique 2,332 Reputation points
2023-05-16T13:25:53.12+00:00

I am developing ASP.NET web application. In this, one of the pages i have several URL which contains below sample.

<li><a href="file://SVRUS1XX/USA/MPS/TEK/STD REMOTE/1.INITIAL/">FIRST TAKE </a></li>

while running the web forms, when i click the URL, nothing will happen...?

How to open this folder while clicking the link...?

Pls. help us to resolve this...

Developer technologies ASP.NET Other
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2023-05-17T17:52:07.97+00:00

    we tried many search in the google. but, couldn't get the exact solution for this.

    You are having a hard time finding a solution because your use case is not possible. You'll need to come up with a different design approach that works with web applications.

    One options is to create a web page that lists the files. Use a standard Web Forms OnClick to download the file. Keep in mind, the web application must have read rights to the file share. Below is a simple example to get you started.

    namespace WebFormsDemo.FilesAndFolders
    {
        public partial class _default : System.Web.UI.Page
        {
            public const string FolderPath = @"C:\Images";
            protected void Page_Load(object sender, EventArgs e)
            {
                FileList.DataSource = GetfilesAndFolder(FolderPath);
                FileList.DataBind();
            }
    
            private string[] GetfilesAndFolder(string path)
            {
                return Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
            }
    
            protected void FileLinkButton_Click(object sender, EventArgs e)
            {
                string path = ((LinkButton)sender).Text;
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", Path.GetFileName(path)));
                Response.TransmitFile(path);
                Response.End();
            }
        }
    }
    
    <asp:Repeater ID="FileList" runat="server">
        <HeaderTemplate>
            <ul>
        </HeaderTemplate>
        <ItemTemplate>
            <ul>
                <asp:LinkButton ID="FileLinkButton" runat="server" Text="<%# Container.DataItem %>" OnClick="FileLinkButton_Click" />
            </ul>
        </ItemTemplate>
        <FooterTemplate>
            </ul>
        </FooterTemplate>
    </asp:Repeater>
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-05-16T15:35:46.7133333+00:00

    browser security has removed support of the file:// protocol, but it never supported folder access only file. if you google you can find samples of exposing a file system as downloadable links.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.