simdownload.aspx.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using BizCom;
  2. using ICSharpCode.SharpZipLib.Zip;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.Data;
  7. using System.IO;
  8. using System.Text;
  9. using System.Web;
  10. using System.Web.UI;
  11. using System.Web.UI.WebControls;
  12. public partial class simdownload : System.Web.UI.Page
  13. {
  14. public static string dPath = ConfigurationManager.AppSettings["dPath"];
  15. public static string copyPath = ConfigurationManager.AppSettings["copyPath"];
  16. private void conErc(string msg)
  17. {
  18. Response.Write("{\"type\":\"error\",\"result\":\"" + msg + "\"}");
  19. //Response.End();
  20. }
  21. private void conSuc(string msg)
  22. {
  23. Response.Write("{\"type\":\"success\",\"result\":\"" + msg + "\"}");
  24. //Response.End();
  25. }
  26. protected void Page_Load(object sender, EventArgs e)
  27. {
  28. if (Request["hexdata"] != null)
  29. {
  30. string tids = Request["hexdata"];
  31. string fpId = Request["hexdata"];
  32. if (tids.Trim() == "")
  33. {
  34. conErc("无法下载");
  35. return;
  36. }
  37. try
  38. {
  39. string[] tArr = tids.Split(',');
  40. List<string> tLst = new List<string>();
  41. foreach (string id in tArr)
  42. {
  43. tLst.Add("'" + id + "'");
  44. }
  45. tids = string.Join(",", tLst.ToArray());
  46. StringBuilder sql = new StringBuilder();
  47. string ut = Request["ut"];
  48. if (ut == "fp")
  49. {
  50. sql.AppendFormat("select tid,img from ce_erpbill where id=" + fpId );
  51. }
  52. DataTable dt = CeErpTradeCell.ExecuteDataset(sql.ToString()).Tables[0];
  53. if (dt == null || dt.Rows.Count < 1)
  54. {
  55. conErc("没有找到相关附件");
  56. return;
  57. }
  58. List<string> files = new List<string>();
  59. string dTime = "";
  60. string fname = "";
  61. int fc = 0;
  62. foreach (DataRow dr in dt.Rows)
  63. {
  64. if (dr["img"].ToString() != "")
  65. {
  66. if(!File.Exists(dPath + "\\" + ut + "\\" + dr["img"].ToString()))
  67. {
  68. conErc("找不到文件:"+ dr["img"].ToString());
  69. return;
  70. }
  71. files.Add(dPath + "\\" + ut + "\\" + dr["img"].ToString());
  72. }
  73. }
  74. if (files.Count < 1)
  75. {
  76. conErc("没有找到相关附件");
  77. return;
  78. }
  79. if(files.Count==1)downLoadFile(files[0]);
  80. else
  81. {
  82. ZipFileDownload(files, "LT_" + DateTime.Now.ToString("yyyyMMddhhMmss") + ".zip");
  83. }
  84. return;
  85. }
  86. catch (Exception ex)
  87. {
  88. conErc("错误的下载访问" + tids + "," + ex.Message);
  89. return;
  90. }
  91. }
  92. }
  93. private string formatMemo(object memo)
  94. {
  95. string m = memo.ToString();
  96. m = m.Replace("\\", "").Replace("/", "").Replace(":", "").Replace("*", "").Replace("?", "").Replace("<", "").Replace(">", "").Replace("|", "");
  97. return m;
  98. }
  99. private void copyFile(string date,string supplier,string file)
  100. {
  101. string path = copyPath + "\\" + date + "\\" + supplier;
  102. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  103. string fname = Path.GetFileName(file);
  104. File.Copy(file, path + "\\" + fname,true);
  105. }
  106. private string getDesignTime(object v)
  107. {
  108. if (v.ToString()=="") return "";
  109. return Convert.ToDateTime(v).ToString("yyyyMM");
  110. }
  111. private string getDesignDate(object v)
  112. {
  113. return DateTime.Now.ToString("yyyy-MM-dd");
  114. if (v.ToString() == "") return "";
  115. return Convert.ToDateTime(v).ToString("yyyy-MM-dd");
  116. }
  117. /// 批量进行多个文件压缩到一个文件
  118. /// </summary>
  119. /// <param name="files">文件列表(绝对路径)</param> 这里用的数组,你可以用list 等或者
  120. /// <param name="zipFileName">生成的zip文件名称</param>
  121. private void ZipFileDownload(List<string> files, string zipFileName)
  122. {
  123. MemoryStream ms = new MemoryStream();
  124. byte[] buffer = null;
  125. using (ZipFile file = ZipFile.Create(ms))
  126. {
  127. file.BeginUpdate();
  128. //file.NameTransform = new ZipNameTransform();
  129. file.NameTransform = new MyNameTransfom();
  130. foreach (var item in files)
  131. {
  132. if (File.Exists(item)) file.Add(item);
  133. }
  134. file.CommitUpdate();
  135. buffer = new byte[ms.Length];
  136. ms.Position = 0;
  137. ms.Read(buffer, 0, buffer.Length); //读取文件内容(1次读ms.Length/1024M)
  138. ms.Flush();
  139. ms.Close();
  140. }
  141. Response.Clear();
  142. Response.Buffer = true;
  143. Response.ContentType = "application/x-zip-compressed";
  144. Response.AddHeader("Set-Cookie", "fileDownload=true; path=/");
  145. Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(zipFileName));
  146. Response.BinaryWrite(buffer);
  147. Page.Response.Flush();
  148. Page.Response.SuppressContent = true;
  149. HttpContext.Current.ApplicationInstance.CompleteRequest();
  150. //ScriptManager.RegisterStartupScript(Page, Page.GetType(), "popup", "alert('123');", true);
  151. //Response.Flush();
  152. //Response.End();
  153. }
  154. private void downLoadFile(string file)
  155. {
  156. string filePath = file;
  157. string dfile = Path.GetFileName(file);
  158. //以字符流的形式下载文件
  159. FileStream fs = new FileStream(filePath, FileMode.Open);
  160. byte[] bytes = new byte[(int)fs.Length];
  161. fs.Read(bytes, 0, bytes.Length);
  162. fs.Close();
  163. Response.ContentType = "application/octet-stream";
  164. //通知浏览器下载文件而不是打开
  165. Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(dfile, System.Text.Encoding.UTF8));
  166. Response.BinaryWrite(bytes);
  167. //Response.Flush();
  168. Response.End();
  169. }
  170. }