asp.net 多個檔案同時下載

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   sp   for   

1、首先讀取檔案夾下的檔案,可能同時存在多個檔案

2、選中檔案,然後點擊下載,同時可以選擇多個檔案。

思路:通過生產壓縮包的形式進行下載,然後再清楚壓縮包,這樣使用者可以一次性全部下載下來。

一、擷取目錄下的所有檔案,然後綁定到checkboxlist中 ,代碼如下:

 ckl_ck.Items.Clear();        DirectoryInfo TheFolder = new DirectoryInfo(Server.MapPath("Resource/Help"));        //遍曆檔案夾下的檔案        foreach (FileInfo NextFile in TheFolder.GetFiles())            this.ckl_ck.Items.Add(NextFile.Name);

二、選中檔案後,點擊下載按鈕。代碼:

 protected void Btn_down_Click(object sender, EventArgs e)    {        if (ckl_ck.Items.Count > 0)        {            List<string> listFJ = new List<string>();//儲存附件路徑            List<string> listFJName = new List<string>();//儲存附件名字            for (int i = 0; i < ckl_ck.Items.Count; i++)            {                if (ckl_ck.Items[i].Selected)                {                    listFJ.Add(Server.MapPath("Resource/Help/") + ckl_ck.Items[i].Text);                    listFJName.Add(ckl_ck.Items[i].Text);                }            }            string time = DateTime.Now.Ticks.ToString();            ZipFileMain(listFJ.ToArray(), listFJName.ToArray(), Server.MapPath("Resource/Help/" + time + ".zip"), 9);//壓縮檔            DownloadFile(Server.UrlEncode("附件.zip"), Server.MapPath("Resource/Help/" + time + ".zip"));//下載檔案        }    }    private void DownloadFile(string fileName, string filePath)    {        FileInfo fileInfo = new FileInfo(filePath);        Response.Clear();        Response.ClearContent();        Response.ClearHeaders();        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);        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();        File.Delete(filePath);//刪除已下載檔案        Response.End();    }    /// <summary>    /// 壓縮檔    /// </summary>    /// <param name="fileName">要壓縮的所有檔案(完全路徑)</param>    /// <param name="fileName">檔案名稱</param>    /// <param name="name">壓縮後檔案路徑</param>    /// <param name="Level">壓縮層級</param>    public void ZipFileMain(string[] filenames, string[] fileName, string name, int Level)    {        ZipOutputStream s = new ZipOutputStream(File.Create(name));        Crc32 crc = new Crc32();        //壓縮層級        s.SetLevel(Level); // 0 - store only to 9 - means best compression        try        {            int m = 0;            foreach (string file in filenames)            {                //開啟壓縮檔                FileStream fs = File.OpenRead(file);//檔案地址                byte[] buffer = new byte[fs.Length];                fs.Read(buffer, 0, buffer.Length);                //建立壓縮實體                ZipEntry entry = new ZipEntry(fileName[m].ToString());//原檔案名稱                //時間                entry.DateTime = DateTime.Now;                //空間大小                entry.Size = fs.Length;                fs.Close();                crc.Reset();                crc.Update(buffer);                entry.Crc = crc.Value;                s.PutNextEntry(entry);                s.Write(buffer, 0, buffer.Length);                m++;            }        }        catch        {            throw;        }        finally        {            s.Finish();            s.Close();        }    }

三、系統中需要引用的dll http://download.csdn.net/detail/njxiaogui/8251031 需要下載。

四、運行效果

asp.net 多個檔案同時下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.