搜索目录中的文件并显示为链接并下载这些文件 [英] Search files in directories and display as a link and download those files

查看:89
本文介绍了搜索目录中的文件并显示为链接并下载这些文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,我的内网网站上的
...我希望将目录和子文件夹中的搜索文件列为链接按钮,显示后我想下载这些文件。

plz任何人都可以帮我解决这个问题..



这个代码在我调试之后它只显示目录中的搜索文件并显示结果。但不喜欢链接butttons。

bcz我写了response.write ...

i想要而不是response.write plz突出显示为链接或超链接当我点击搜索到的文件文件应该下载。

plz帮助它非常紧急...





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...


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");
            }
        }
    }
}

推荐答案

试试这个

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



希望这有助于


hope this helps


您好,



您可以使用ASHX文件(例如,downloadfile.ashx)并使用以下代码(未经过测试,但它会是这样的)其中:



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:

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:

<a href="downloadfile.ashx"  target=""_blank"">Click me</a>



注意:您还可以传递参数以下载不同的文件,例如:


Note: You can also pass parameters for downloading different files like:

<a href="downloadfile.ashx?file=abc.txt"  target=""_blank"">Click me</a>



然后,在ashx文件中,使用文件名下载相应的文件。



然后,替换以下代码


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 ;)


这篇关于搜索目录中的文件并显示为链接并下载这些文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆