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 页面集_科学成绩表 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindListBox();
        }
    }
    public void DownLoad(string filePath)
    {
        //bool re = false;
        byte[] buffer = new Byte[1024];
        //string filePath = @"E:\WebSite27\File\ew.doc";
        string fileName = Path.GetFileName(filePath);
        Stream fileStream = null;
        try
        {
            fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            Response.Clear();
            long fileSize = fileStream.Length;
            long sum = 0;
            if (Request.Headers["Range"] != null)
            {
                Response.StatusCode = 206;
                sum = long.Parse(Request.Headers["Range"].Replace("bytes=", "").Replace("-", ""));
            }
            if (sum != 0)
            {
                Response.AddHeader("Content-Length", "bytes" + sum.ToString() + "-" + ((long)(fileSize)).ToString() + "/" + fileSize.ToString());
            }
            Response.AddHeader("Content-Length", ((long)(fileSize - sum)).ToString());
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(fileName)));
            fileStream.Position = sum;
            fileSize = fileSize - sum;
            while (fileSize > 0)
            {
                if (Response.IsClientConnected)
                {
                    int length = fileStream.Read(buffer, 0, 1024);
                    Response.OutputStream.Write(buffer, 0, length);
                    Response.Flush();
                    buffer = new Byte[1024];
                    fileSize = fileSize - length;
                }
                else
                {
                    fileSize = -1;
                }
            }
            //re = true;
        }
        catch (Exception ex)
        {
            Response.Write("Errror:" + ex.Message);
        }
        finally
        {
            if (fileStream != null)
            {
                fileStream.Close();
            }
            Response.End();
        }
        //return re;
    }
    private void BindListBox()
    {
        listboxkx.Items.Clear();
        string path = Server.MapPath("../科学");
        string[] files = System.IO.Directory.GetFiles(path);
        foreach (string f in files)
        {
            string fileName = System.IO.Path.GetFileName(f);
            ListItem li = new ListItem(fileName, f);
            listboxkx.Items.Add(li);
        }
    }
    protected void btnkxopen_Click(object sender, EventArgs e)
    {
        if (listboxkx.SelectedIndex == -1)
        {
            Response.Write("<script>alert('请先选定要下载的班级学科表！');</script>");
            return;
        }
        string path = Server.MapPath("../") + "\\" + "科学" + "\\" + listboxkx.SelectedItem.ToString();
        DownLoad(path);    
    }
    protected void btmbkxup_Click(object sender, EventArgs e)
    {
        try
        {
            string upName = fulbxqkx.FileName;
            if ("" == upName)
            {
                return;
            }
            //string nameLast = upName.Substring(upName.LastIndexOf("."));
            //string fileName = txtName.Text + nameLast;
            string path = Server.MapPath("../") + "科学成绩表" + "\\" + upName;
            fulbxqkx.PostedFile.SaveAs(path);
            RegisterStartupScript("true", "<script>alert('上传成功！')</script>");
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
            RegisterStartupScript("true", "<script>alert('上传失败！')</script>");
        }
    }
}
