testcopydownload.aspx.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using BizCom;
  2. using ICSharpCode.SharpZipLib.Zip;
  3. using SQLData;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Configuration;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.IO;
  10. using System.Text;
  11. using System.Web;
  12. using System.Web.UI;
  13. using System.Web.UI.WebControls;
  14. public partial class testcopydownload : System.Web.UI.Page
  15. {
  16. public static string upPath = ConfigurationManager.AppSettings["upPath"];
  17. public static string copyPath = ConfigurationManager.AppSettings["copyPath"];
  18. private void conErc(string msg)
  19. {
  20. Response.Write("{\"type\":\"error\",\"result\":\"" + msg + "\"}");
  21. //Response.End();
  22. }
  23. private void conSuc(string msg)
  24. {
  25. Response.Write("{\"type\":\"success\",\"result\":\"" + msg + "\"}");
  26. //Response.End();
  27. }
  28. protected void Page_Load(object sender, EventArgs e)
  29. {
  30. if (Request["hexdata"]!=null)
  31. {
  32. string tids = Request["hexdata"];
  33. if (tids.Trim() == "")
  34. {
  35. conErc("无法下载");
  36. return;
  37. }
  38. int userId = 0;
  39. if (Request["userid"] != null)
  40. {
  41. userId = Convert.ToInt32(Request["userid"]);
  42. }
  43. try
  44. {
  45. string[] tArr = tids.Split(',');
  46. List<string> tLst = new List<string>();
  47. foreach (string id in tArr)
  48. {
  49. tLst.Add("'" + id + "'");
  50. }
  51. tids = string.Join(",", tLst.ToArray());
  52. StringBuilder sql = new StringBuilder();
  53. sql.AppendFormat("select ctid,seller_memo,FinishDesignTime,OrderState,SupplierName from view_erptradecell where orderstate=5 and ctid in ({0})", tids);
  54. DataTable dt = CeErpTradeCell.ExecuteDataset(sql.ToString()).Tables[0];
  55. if (dt == null || dt.Rows.Count < 1)
  56. {
  57. conErc("没有找到相关订单");
  58. return;
  59. }
  60. List<string> files = new List<string>();
  61. string dTime = "";
  62. string fname = "";
  63. int fc = 0;
  64. bool hasFile = false;
  65. string df_name = "";
  66. string[] extArr = new string[] { ".cdr", ".zip",".rar" };
  67. List<string> noFileLst = new List<string>();
  68. List<string> errorFileLst = new List<string>();
  69. foreach (DataRow dr in dt.Rows)
  70. {
  71. if (dr["FinishDesignTime"].ToString() == "") continue;
  72. hasFile = false;
  73. dTime = getDesignTime(dr["FinishDesignTime"]);
  74. df_name = upPath + "\\" + dTime + "\\" + formatMemo(dr["seller_memo"]);
  75. foreach (string ext in extArr)
  76. {
  77. fname = df_name + ext;
  78. if (!hasFile)
  79. {
  80. if (File.Exists(fname))
  81. {
  82. //bool isUpdateError = false;
  83. //try
  84. //{
  85. // SqlParameter[] sqlParameter ={
  86. // new SqlParameter("@mainctids", SqlDbType.VarChar,500),
  87. // new SqlParameter("@userid", SqlDbType.VarChar,4),
  88. // new SqlParameter("@res", SqlDbType.VarChar, 4000)
  89. // };
  90. // sqlParameter[0].Value = dr["ctid"].ToString();
  91. // sqlParameter[1].Value = userId;
  92. // sqlParameter[2].Direction = ParameterDirection.Output;
  93. // CeErpTradeCell.ExecuteDataSetStore("sp_set_download", sqlParameter);
  94. //}
  95. //catch (Exception ex)
  96. //{
  97. // isUpdateError = true;
  98. // errorFileLst.Add("'" + dr["ctid"].ToString() + "'");
  99. // XLog.SaveLog(0, "下载发生错误,ctid:" + dr["ctid"].ToString() + "," + ex.Message);
  100. //}
  101. //if (!isUpdateError)
  102. //{
  103. hasFile = true;
  104. files.Add(fname);
  105. copyFile( fname);
  106. fc++;
  107. //}
  108. }
  109. }
  110. else
  111. {
  112. break;
  113. }
  114. }
  115. if (!hasFile)
  116. {
  117. noFileLst.Add("'" + dr["ctid"].ToString() + "'");
  118. //sql = new StringBuilder();
  119. //sql.AppendFormat("insert into CE_ErpTradeLog(tid,orderstate,userid,operatetime,con) select ctid,{1},{2},getdate(),'{3}' from ce_erptradecell where ctid='{0}' ;", dr["ctid"], (int)OrderState.下单完成, 0, "找不到设计文件");
  120. //CeErpTradeCell.ExecuteNonQuery(sql.ToString());
  121. //conErc("找不到文件,订单号:" + dr["ctid"]);
  122. //return;
  123. }
  124. //if (hasFile)
  125. //{
  126. // fname = upPath + "\\" + dTime + "\\" + formatMemo(dr["seller_memo"]) + ".png";
  127. // if (File.Exists(fname))
  128. // {
  129. // files.Add(fname);
  130. // copyFile(getDesignDate(dr["FinishDesignTime"]), dr["SupplierName"].ToString(), fname);
  131. // }
  132. //}
  133. }
  134. if (fc == 0)
  135. {
  136. conErc("没有找到相关的设计附件");
  137. return;
  138. }
  139. //sql = new StringBuilder();
  140. //sql.AppendFormat("insert into CE_ErpTradeLog(tid,orderstate,userid,operatetime,con) select ctid,{1},{2},getdate(),'{3}' from ce_erptradecell where ctid in ({0}) ;", tids, (int)OrderState.下单完成, userId, "下载设计文件");
  141. //sql.AppendFormat("update ce_erptradecell set FinishPlaceTime=getdate(),OrderState={0},IsReturn=0 where ctid in ({1}) and orderstate={2} and IsReturn<>2 and seller_memo not like '%电子稿%' ;", (int)OrderState.下单完成, tids, (int)OrderState.设计完成);
  142. //sql.AppendFormat("update ce_erptradecell set FinishPlaceTime=getdate(),OrderState={0},IsReturn=0 where ctid in ({1}) and orderstate={2} and IsReturn<>2 and seller_memo like '%电子稿%' ;", (int)OrderState.已发货, tids, (int)OrderState.设计完成);
  143. //CeErpTradeCell.ExecuteNonQuery(sql.ToString());
  144. foreach (string ctid in tArr)
  145. {
  146. CeErpTradeCell.UpdateRelationOrder(ctid);
  147. }
  148. if (Request["supplier"] != null && Request["supplier"].ToString() == "1")
  149. {
  150. ZipFileDownload(files, "LT_" + DateTime.Now.ToString("yyyyMMddhhMmss") + ".zip");
  151. }
  152. else if (noFileLst.Count > 0 || errorFileLst.Count > 0)
  153. {
  154. string tips = "";
  155. if (noFileLst.Count > 0)
  156. {
  157. string notids = string.Join(",", noFileLst.ToArray());
  158. //sql = new StringBuilder();
  159. //sql.AppendFormat("insert into CE_ErpTradeLog(tid,orderstate,userid,operatetime,con) select ctid,{1},{2},getdate(),'{3}' from ce_erptradecell where ctid in ({0}) ;", notids, (int)OrderState.下单完成, 0, "找不到设计文件");
  160. //CeErpTradeCell.ExecuteNonQuery(sql.ToString());
  161. tips += "找不到文件,单号:" + notids;
  162. }
  163. if (errorFileLst.Count > 0)
  164. {
  165. string ertids = string.Join(",", errorFileLst.ToArray());
  166. //sql = new StringBuilder();
  167. //sql.AppendFormat("insert into CE_ErpTradeLog(tid,orderstate,userid,operatetime,con) select ctid,{1},{2},getdate(),'{3}' from ce_erptradecell where ctid in ({0}) ;", ertids, (int)OrderState.下单完成, 0, "下载设计文件出错");
  168. //CeErpTradeCell.ExecuteNonQuery(sql.ToString());
  169. tips += " 下载出错,单号:" + ertids;
  170. }
  171. conErc(tips);
  172. }
  173. else
  174. {
  175. conSuc("文件已下载到共享目录");
  176. }
  177. return;
  178. }
  179. catch(Exception ex)
  180. {
  181. conErc("错误的下载访问");
  182. XLog.SaveLog(0, "copydown"+ex.Message);
  183. return;
  184. }
  185. }
  186. }
  187. private string formatMemo(object memo)
  188. {
  189. string m = memo.ToString();
  190. m = m.Replace("\\", "").Replace("/", "").Replace(":", "").Replace("*", "").Replace("?", "").Replace("<", "").Replace(">", "").Replace("|", "");
  191. return m;
  192. }
  193. private void copyFile(string file)
  194. {
  195. try
  196. {
  197. string path = copyPath + "\\" + "testcopy" ;
  198. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  199. string fname = Path.GetFileName(file);
  200. File.Copy(file, path + "\\" + fname, true);
  201. //if(!File.Exists(path + "\\" + fname))
  202. //{
  203. // File.Copy(file, path + "\\" + fname, true);
  204. //}
  205. }
  206. catch(Exception ex)
  207. {
  208. XLog.SaveLog(0, "复制文件发生错误," + file + "," + ex.Message);
  209. }
  210. }
  211. private string getDesignTime(object v)
  212. {
  213. if (v.ToString()=="") return "";
  214. return Convert.ToDateTime(v).ToString("yyyyMM");
  215. }
  216. private string getDesignDate(object v)
  217. {
  218. return DateTime.Now.ToString("yyyy-MM-dd");
  219. if (v.ToString() == "") return "";
  220. return Convert.ToDateTime(v).ToString("yyyy-MM-dd");
  221. }
  222. /// 批量进行多个文件压缩到一个文件
  223. /// </summary>
  224. /// <param name="files">文件列表(绝对路径)</param> 这里用的数组,你可以用list 等或者
  225. /// <param name="zipFileName">生成的zip文件名称</param>
  226. private void ZipFileDownload(List<string> files, string zipFileName)
  227. {
  228. MemoryStream ms = new MemoryStream();
  229. byte[] buffer = null;
  230. using (ZipFile file = ZipFile.Create(ms))
  231. {
  232. file.BeginUpdate();
  233. //file.NameTransform = new ZipNameTransform();
  234. file.NameTransform = new MyNameTransfom();
  235. foreach (var item in files)
  236. {
  237. if (File.Exists(item)) file.Add(item);
  238. }
  239. file.CommitUpdate();
  240. buffer = new byte[ms.Length];
  241. ms.Position = 0;
  242. ms.Read(buffer, 0, buffer.Length); //读取文件内容(1次读ms.Length/1024M)
  243. ms.Flush();
  244. ms.Close();
  245. }
  246. Response.Clear();
  247. Response.Buffer = true;
  248. Response.ContentType = "application/x-zip-compressed";
  249. Response.AddHeader("Set-Cookie", "fileDownload=true; path=/");
  250. Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(zipFileName));
  251. Response.BinaryWrite(buffer);
  252. Page.Response.Flush();
  253. Page.Response.SuppressContent = true;
  254. HttpContext.Current.ApplicationInstance.CompleteRequest();
  255. //ScriptManager.RegisterStartupScript(Page, Page.GetType(), "popup", "alert('123');", true);
  256. //Response.Flush();
  257. //Response.End();
  258. }
  259. private void downLoadFile(string file)
  260. {
  261. string filePath = Server.MapPath("d/"+file);//路径
  262. //以字符流的形式下载文件
  263. FileStream fs = new FileStream(filePath, FileMode.Open);
  264. byte[] bytes = new byte[(int)fs.Length];
  265. fs.Read(bytes, 0, bytes.Length);
  266. fs.Close();
  267. Response.ContentType = "application/octet-stream";
  268. //通知浏览器下载文件而不是打开
  269. Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file, System.Text.Encoding.UTF8));
  270. Response.BinaryWrite(bytes);
  271. Response.Flush();
  272. Response.End();
  273. }
  274. }