Click here to Skip to main content
16,004,678 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
in my intranet website...i want to list searched files from directory and sub folders as a linked buttons and after displaying i want to download those files.
plz any one help me in this issue..

here in this code after i debug it only shows searched files from directory and it displays the result ..but not like link butttons.
bcz i have written response.write...
i want instead of response.write plz highlight in as a link or hyperlinks and when i click the searched files file should download.
plz help its very urgent...


C#
public partial class SEARCH : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        {
            string search = TextBox1.Text;
            string[] files = Directory.GetFiles(@"E:\\DAL\", "*.*", SearchOption.AllDirectories);
            int Flag = 0;
            string dir = @"E:\\DAL\";
            string[] files1;
            int numFiles;
            files1 = Directory.GetFiles(dir);

            numFiles = files.Length;
            Response.Write("Files searched : " + numFiles + "<br>");
            for (int i = 0; i < numFiles; i++)
            {


                string file = files[i].ToString();

                int file1 = file.IndexOf(search, StringComparison.CurrentCultureIgnoreCase);
                if (file1 != -1)
                {
                                      Response.Write("<br>" + file);
                  
                    Flag = 1;


                }

            }
            if (Flag == 0)
            {
                Response.Write("No Matches Found");
            }
        }
    }
}
Posted

try this
C#
Response.WriteFile(Server.MapPath(file));

hope this helps
 
Share this answer
 
Hello,

You can use ASHX file (say, downloadfile.ashx) and use the following code (not tested, but it will be something like that) in it:

C#
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=abc.txt");
Response.WriteFile(Server.MapPath("\\servername\folder1\folder2\folder3\abc.txt"));
Response.End();


and then use this in your anchor tag like:
HTML
<a href="downloadfile.ashx"  target=""_blank"">Click me</a>

Note: You can also pass parameters for downloading different files like:
HTML
<a href="downloadfile.ashx?file=abc.txt"  target=""_blank"">Click me</a>

and then, in ashx file, use the file name to download the appropriate file.

Then, replace your following code
Response.Write("<br>" + file);

by something like this
Response.Write("<br><a href="downloadfile.ashx?file=" + file + "">" + file + "</a>");</pre>


You'll maybe need to change your filepath from absolute to relative.

Hope it helps ;)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900