后台下载

using System;
using System.Collections;
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 customer_detail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FileDownload();
    }

    private void FileDownload()
    {
        int id = 0;
        if (Request.QueryString["id"] != null)
        {
            int.TryParse(Request.QueryString["id"].ToString(), out id);
        }
        if (id > 0)
        {
            BLL.DownLoad dlBll = new BLL.DownLoad();
            Model.DownLoad m = dlBll.GetModelBystrWhere("ID=" + id);
            if (m != null && System.IO.File.Exists(Server.MapPath(m.Information)))
            {
                Model.DownLoad model = new Model.DownLoad();
                model.num = m.num + 1;
                dlBll.Update(model, "ID=" + id);
                FileInfo file = new FileInfo(Server.MapPath(m.Information));
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "attachment;filename="
                    + HttpUtility.UrlEncode(m.Information, System.Text.Encoding.UTF8));
                Response.AppendHeader("Content-Length", file.Length.ToString());
                Response.WriteFile(file.FullName);
                Response.Flush();
                Response.End();
                Response.Write("down");
            }
            else
            {
                Error("文件未找到");
            }
        }
        else
        {
            Error("请求参数错误");
        }
    }
    public void Error(string msg)
    {
        Response.Write(msg);
    }
}

 

posted @ 2012-11-13 17:07  一千零一夜  阅读(166)  评论(0编辑  收藏  举报