代码下载文件

use*_*942 2 c# asp.net

我必须在我的网站上提供一个选项来上传多个文件,然后允许用户下载这些文件.我已经完成了上传多个文件的部分,但是我不太清楚我将如何下载部分.我首先想到将动态超链接添加到每个文件的标签上(因为我不确定用户将上传多少文件).但它然后在浏览器中打开文件,并没有提供保存或打开文件的选项.主要问题是用户可以提交任何类型的文件,如ms doc或xls或文本文件等.因此内容类型不固定.

我不清楚我将如何做到这一点我的意思是动态添加链接按钮或动态添加超链接.之后我将如何下载文件?我无法做到

Response.WriteFile(Server.MapPath(@"~/logo_large.gif"));
Run Code Online (Sandbox Code Playgroud)

因为内容类型不明确.关于下载所有类型文件的代码,请帮助我

st *_*nmn 6

下载文件:

    public void DownLoad(string FName)
    {
        string path = FName;
        System.IO.FileInfo file = new System.IO.FileInfo(path);
        if (file.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.End();

        }
        else
        {
            Response.Write("This file does not exist.");
        }

    }
Run Code Online (Sandbox Code Playgroud)

但这是word doc/docx文件.在行中:Response.ContentType = "application/octet-stream";您必须定义文件的类型.

用于显示超链接,动态编写循环并遍历用户上传的所有文件,并使用以下代码:

     yourdivId += "<a href='" + file.FullName + "' >" + file.Name + "</a></br>";
Run Code Online (Sandbox Code Playgroud)