vishal
vishal

Reputation: 1

how to open a file using anchor tags from local file system in asp.net

This function is fetching all the files from the specified directory and showing them as links on the webpage. but on clicking the link, browser is not opening the page, instead showing - " Server Error in '/fileUpload' Application. HTTP ERROR 400 - bad request" I have tried all the possible combinations and ways for giving relative and absolute path. but problem persists. please help..

here is the function

protected void fetchDirFiles(string pattern)

{
string file_content;

    string keyword = pattern; // pattern to be searched in files

    try
    {
        pattern = pattern + "*";

        string[] dirs = Directory.GetFiles(@"C:\\uploaded", "*");

        foreach (string dir in dirs)
        {

            file_content = File.ReadAllText(dir);
            string filename;
            if (file_content.Contains(keyword))
            {
                filename = Path.GetFileName(dir);
                Literal lit = new Literal();
                string root = Path.GetPathRoot(dir); //

                lit.Text = "<a href ='c://uploaded'>" + filename + "</a><br/>";
                this.form1.Controls.Add(lit);
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("The process failed: {0}", e.ToString());
    }

Upvotes: 0

Views: 1379

Answers (1)

Manoj Purohit
Manoj Purohit

Reputation: 3453

try adding file:/// to

lit.Text = "<a href ='file:///c://uploaded'>" + filename + "</a><br/>";

Upvotes: 1

Related Questions