在C#中显示下载选项

时间:2009-04-21 11:08:21

标签: asp.net c#-2.0

点击asp.net按钮后,我需要下载一个Access文件(Inbox.mdb)文件......? 我怎么做这个是C#/ ASP.NET 任何帮助,将不胜感激。

2 个答案:

答案 0 :(得分:1)

<!-- in your aspx file -->
<asp:button id="btnDownload" runat="server" onclick="btnDownload_Click" text="Download Your MDB" />

// and then in your codebehind file
protected void btnDownload_Click(object sender, EventArgs e)
{
    string pathToYourMDB = @"c:\stuff\test.mdb";
    string downloadName = "YourData.mdb";

    Response.Clear();
    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition", "attachment; filename=\"" + downloadName+ "\"");
    Response.TransmitFile(pathToYourMDB);
    Response.End();
}

答案 1 :(得分:0)

如果文件只是在您的服务器上公开托管,则您不需要使用任何C#/ ASP.NET功能。

只需添加一个普通的

<a href="~/Path/To/Inbox.mdb">Link</a>

如果必须使用服务器端按钮,则

Response.Redirect("~/Path/To/Inbox.mdb"); 

将重定向到该文件并导致浏览器下载。

如果它存储在其他位置(即不可公开访问),那么您需要从服务器上的位置流式传输它。通过Response对象在google上查看流媒体文件的示例。