webform生成rar文件並下載

 protected void btnRar_Click(object sender, EventArgs e)
        {
            try
            {
                string path = Server.MapPath("/");
                string message = Rar(path + @"/uploads", @"pic/*.jpg", "", "tupian");
                string filePath = path + @"/uploads/tupian.rar";
                System.IO.FileInfo file = new System.IO.FileInfo(filePath);
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解決中文亂碼
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解決中文文件名亂碼   
                Response.AddHeader("Content-length", file.Length.ToString());
                //Response.ContentType = "appliction/octet-stream";
                Response.WriteFile(file.FullName);
                Response.End();


            }
            catch (Exception ex)
            {
                Page.RegisterStartupScript("key", "<script> alert('" + ex.Message + "');</script>");
            }
           
        }
        string Rar(string workDir, string sourceDir, string destDir, string destFileName)
        {
            string error = string.Empty;
            Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(@"Applications/WinRAR.exe/Shell/Open/Command");
            string rarExeFile = regKey.GetValue("").ToString().Substring(1, regKey.GetValue("").ToString().Length - 7);
            regKey.Close();
            System.Diagnostics.ProcessStartInfo pInfo = new System.Diagnostics.ProcessStartInfo();
            pInfo.FileName = rarExeFile;
            pInfo.Arguments = string.Format("a  -rr {0}.rar {1}", destFileName, sourceDir);
            pInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            pInfo.WorkingDirectory = workDir;
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo = pInfo;
            process.Start();
            error = "壓縮成功";
            return error;

        }

==============================================

發佈了29 篇原創文章 · 獲贊 3 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章