using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;public partial class ja1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {    }
    protected void btnxiazai_Click(object sender, EventArgs e)
    {
        string strfilename = "Lab11A.rar";
        string strfilepath = Server.MapPath("File/Lab11A.rar");
        FileInfo fileinfo = new FileInfo(strfilepath);
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "attachment;strfilename=" + strfilename);
        Response.AddHeader("Content-Length",fileinfo.Length.ToString());
        Response.AddHeader("Content-Transfer-Encoding","binary");
        Response.ContentType = "application/octet-stream";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.WriteFile(fileinfo.FullName);
        Response.Flush();
        Response.End();
    }
}
======================================================================================================================
为什么运行后能下载的是这个代码页面而不是Lab11A.rar?忘高手赐教。

解决方案 »

  1.   

    string name ="Lab11A.rar"; 
            Response.ContentType = "application/ms-download";
            System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("upload/" + name));
            Response.Clear();
            Response.AddHeader("Content-Type", "application/octet-stream");
            Response.Charset = "utf-8";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.WriteFile(file.FullName);
            Response.Flush();
            Response.Clear();
            Response.End();