<a href="#">点击下载此文件</a>  怎么实现 ? 指定的文件 比如 点击链接下载 a.pdf

解决方案 »

  1.   

    <a href="a.pdf">点击下载此文件</a>
      

  2.   


    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"  Debug="true"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>
     </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <a href="#" onclick="location='?id=1';">下载</a>
        </div>
        </form>
    </body>
    </html>
    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.IO;public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["id"] != null)
            {
                //可以先判断是不是从本页面点击下载的,不是的话就不准下载了
                //string id = "1";//对应一个下载文件的编号            if (Request.Params["id"].ToString()=="1")
                    DownLoad(@"e:\1.txt");
            }
        }    private void DownLoad(string filePath)
        {
            Response.Clear();
            string fileName = Path.GetFileName(filePath);
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlDecode(fileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }
    }
      

  3.   

    如果指定是要下载 pdf 文件
    可以把Response.ContentType = "application/octet-stream";
    改成 Response.ContentType = "application/pdf";这样如果你的浏览器装了pdf文件查看器,就支持直接打开了,我们一般文件打印就是这样做的
      

  4.   

    恩,应该是我装了 pdf查看器 。就直接链接就可以了。