ファイルをダウンロードします

6308 ワード

使用するdll:ICSharpCode.SharpZipLib
ダウンロード  http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
 

/// <summary>

///     

/// </summary>

public void DownloadCourseData()

{

    string attachBatchNo = DESHelper.Decrypt(ctx.Get("no"), "simpo");//     

    List<Edu_Attach> edu_AttachList = edu_AttachService.FindAttByNo(attachBatchNo);//      

    if (edu_AttachList.Count > 0)

    {

        string pathName = edu_AttachList[0].AttachContent;//    

        int pos = pathName.LastIndexOf("/");

        string path = pathName.Substring(0, pos + 1);//       

        string zipName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + strUtil.GetRnd(4, true, false, false, false, "") + ".zip";//zip   

        string zipPathName = PathHelper.Map(sys.Path.DiskPhoto) + path.Replace("/static/upload/image", "").Replace("/", "\\") + zipName;

        FileStream fs = new FileStream(zipPathName, FileMode.Create);

        ZipOutputStream zos = new ZipOutputStream(fs);

        foreach (Edu_Attach edu_Attach in edu_AttachList)

        {

            string filePathName = PathHelper.Map(sys.Path.DiskPhoto) + edu_Attach.AttachContent.Replace("/static/upload/image", "").Replace("/", "\\");

            pos = filePathName.LastIndexOf("\\");

            string zipEntryName = edu_Attach.AttachName;

            ZipEntry zipEntry = new ZipEntry(zipEntryName);

            zos.PutNextEntry(zipEntry);

            fs = File.OpenRead(filePathName);

            byte[] byteArray = new byte[fs.Length];

            fs.Read(byteArray, 0, byteArray.Length);

            zos.Write(byteArray, 0, byteArray.Length);

        }

        zos.Finish();

        zos.Close();

        string downloadFileName = path + zipName;

        redirectUrl(downloadFileName);

    }

}
View Code