| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- using BizCom;
- using ICSharpCode.SharpZipLib.Zip;
- using SQLData;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.IO;
- using System.Text;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class testcopydownload : System.Web.UI.Page
- {
- public static string upPath = ConfigurationManager.AppSettings["upPath"];
- public static string copyPath = ConfigurationManager.AppSettings["copyPath"];
- private void conErc(string msg)
- {
- Response.Write("{\"type\":\"error\",\"result\":\"" + msg + "\"}");
- //Response.End();
- }
- private void conSuc(string msg)
- {
- Response.Write("{\"type\":\"success\",\"result\":\"" + msg + "\"}");
- //Response.End();
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Request["hexdata"]!=null)
- {
-
- string tids = Request["hexdata"];
- if (tids.Trim() == "")
- {
- conErc("无法下载");
- return;
- }
- int userId = 0;
- if (Request["userid"] != null)
- {
- userId = Convert.ToInt32(Request["userid"]);
- }
- try
- {
- string[] tArr = tids.Split(',');
- List<string> tLst = new List<string>();
- foreach (string id in tArr)
- {
- tLst.Add("'" + id + "'");
- }
- tids = string.Join(",", tLst.ToArray());
- StringBuilder sql = new StringBuilder();
- sql.AppendFormat("select ctid,seller_memo,FinishDesignTime,OrderState,SupplierName from view_erptradecell where orderstate=5 and ctid in ({0})", tids);
- DataTable dt = CeErpTradeCell.ExecuteDataset(sql.ToString()).Tables[0];
- if (dt == null || dt.Rows.Count < 1)
- {
- conErc("没有找到相关订单");
- return;
- }
- List<string> files = new List<string>();
- string dTime = "";
- string fname = "";
- int fc = 0;
- bool hasFile = false;
- string df_name = "";
- string[] extArr = new string[] { ".cdr", ".zip",".rar" };
- List<string> noFileLst = new List<string>();
- List<string> errorFileLst = new List<string>();
- foreach (DataRow dr in dt.Rows)
- {
- if (dr["FinishDesignTime"].ToString() == "") continue;
- hasFile = false;
- dTime = getDesignTime(dr["FinishDesignTime"]);
- df_name = upPath + "\\" + dTime + "\\" + formatMemo(dr["seller_memo"]);
- foreach (string ext in extArr)
- {
- fname = df_name + ext;
- if (!hasFile)
- {
- if (File.Exists(fname))
- {
- //bool isUpdateError = false;
- //try
- //{
- // SqlParameter[] sqlParameter ={
- // new SqlParameter("@mainctids", SqlDbType.VarChar,500),
- // new SqlParameter("@userid", SqlDbType.VarChar,4),
- // new SqlParameter("@res", SqlDbType.VarChar, 4000)
- // };
- // sqlParameter[0].Value = dr["ctid"].ToString();
- // sqlParameter[1].Value = userId;
- // sqlParameter[2].Direction = ParameterDirection.Output;
- // CeErpTradeCell.ExecuteDataSetStore("sp_set_download", sqlParameter);
- //}
- //catch (Exception ex)
- //{
- // isUpdateError = true;
- // errorFileLst.Add("'" + dr["ctid"].ToString() + "'");
- // XLog.SaveLog(0, "下载发生错误,ctid:" + dr["ctid"].ToString() + "," + ex.Message);
- //}
- //if (!isUpdateError)
- //{
- hasFile = true;
- files.Add(fname);
- copyFile( fname);
- fc++;
- //}
- }
- }
- else
- {
- break;
- }
- }
- if (!hasFile)
- {
- noFileLst.Add("'" + dr["ctid"].ToString() + "'");
- //sql = new StringBuilder();
- //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, "找不到设计文件");
- //CeErpTradeCell.ExecuteNonQuery(sql.ToString());
- //conErc("找不到文件,订单号:" + dr["ctid"]);
- //return;
- }
- //if (hasFile)
- //{
- // fname = upPath + "\\" + dTime + "\\" + formatMemo(dr["seller_memo"]) + ".png";
- // if (File.Exists(fname))
- // {
- // files.Add(fname);
- // copyFile(getDesignDate(dr["FinishDesignTime"]), dr["SupplierName"].ToString(), fname);
- // }
- //}
-
- }
- if (fc == 0)
- {
- conErc("没有找到相关的设计附件");
- return;
- }
- //sql = new StringBuilder();
- //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, "下载设计文件");
- //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.设计完成);
- //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.设计完成);
- //CeErpTradeCell.ExecuteNonQuery(sql.ToString());
- foreach (string ctid in tArr)
- {
- CeErpTradeCell.UpdateRelationOrder(ctid);
- }
- if (Request["supplier"] != null && Request["supplier"].ToString() == "1")
- {
- ZipFileDownload(files, "LT_" + DateTime.Now.ToString("yyyyMMddhhMmss") + ".zip");
- }
- else if (noFileLst.Count > 0 || errorFileLst.Count > 0)
- {
- string tips = "";
- if (noFileLst.Count > 0)
- {
- string notids = string.Join(",", noFileLst.ToArray());
- //sql = new StringBuilder();
- //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, "找不到设计文件");
- //CeErpTradeCell.ExecuteNonQuery(sql.ToString());
- tips += "找不到文件,单号:" + notids;
- }
- if (errorFileLst.Count > 0)
- {
- string ertids = string.Join(",", errorFileLst.ToArray());
- //sql = new StringBuilder();
- //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, "下载设计文件出错");
- //CeErpTradeCell.ExecuteNonQuery(sql.ToString());
- tips += " 下载出错,单号:" + ertids;
- }
- conErc(tips);
- }
- else
- {
- conSuc("文件已下载到共享目录");
- }
-
- return;
- }
- catch(Exception ex)
- {
- conErc("错误的下载访问");
- XLog.SaveLog(0, "copydown"+ex.Message);
- return;
- }
- }
- }
- private string formatMemo(object memo)
- {
- string m = memo.ToString();
- m = m.Replace("\\", "").Replace("/", "").Replace(":", "").Replace("*", "").Replace("?", "").Replace("<", "").Replace(">", "").Replace("|", "");
- return m;
- }
- private void copyFile(string file)
- {
- try
- {
- string path = copyPath + "\\" + "testcopy" ;
- if (!Directory.Exists(path)) Directory.CreateDirectory(path);
- string fname = Path.GetFileName(file);
- File.Copy(file, path + "\\" + fname, true);
- //if(!File.Exists(path + "\\" + fname))
- //{
- // File.Copy(file, path + "\\" + fname, true);
- //}
- }
- catch(Exception ex)
- {
- XLog.SaveLog(0, "复制文件发生错误," + file + "," + ex.Message);
- }
-
- }
- private string getDesignTime(object v)
- {
- if (v.ToString()=="") return "";
- return Convert.ToDateTime(v).ToString("yyyyMM");
- }
- private string getDesignDate(object v)
- {
- return DateTime.Now.ToString("yyyy-MM-dd");
- if (v.ToString() == "") return "";
- return Convert.ToDateTime(v).ToString("yyyy-MM-dd");
- }
- /// 批量进行多个文件压缩到一个文件
- /// </summary>
- /// <param name="files">文件列表(绝对路径)</param> 这里用的数组,你可以用list 等或者
- /// <param name="zipFileName">生成的zip文件名称</param>
- private void ZipFileDownload(List<string> files, string zipFileName)
- {
- MemoryStream ms = new MemoryStream();
- byte[] buffer = null;
- using (ZipFile file = ZipFile.Create(ms))
- {
- file.BeginUpdate();
- //file.NameTransform = new ZipNameTransform();
- file.NameTransform = new MyNameTransfom();
- foreach (var item in files)
- {
- if (File.Exists(item)) file.Add(item);
- }
- file.CommitUpdate();
- buffer = new byte[ms.Length];
- ms.Position = 0;
- ms.Read(buffer, 0, buffer.Length); //读取文件内容(1次读ms.Length/1024M)
- ms.Flush();
- ms.Close();
- }
- Response.Clear();
- Response.Buffer = true;
- Response.ContentType = "application/x-zip-compressed";
- Response.AddHeader("Set-Cookie", "fileDownload=true; path=/");
- Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(zipFileName));
- Response.BinaryWrite(buffer);
- Page.Response.Flush();
- Page.Response.SuppressContent = true;
- HttpContext.Current.ApplicationInstance.CompleteRequest();
- //ScriptManager.RegisterStartupScript(Page, Page.GetType(), "popup", "alert('123');", true);
- //Response.Flush();
- //Response.End();
- }
- private void downLoadFile(string file)
- {
- string filePath = Server.MapPath("d/"+file);//路径
- //以字符流的形式下载文件
- FileStream fs = new FileStream(filePath, FileMode.Open);
- byte[] bytes = new byte[(int)fs.Length];
- fs.Read(bytes, 0, bytes.Length);
- fs.Close();
- Response.ContentType = "application/octet-stream";
- //通知浏览器下载文件而不是打开
- Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file, System.Text.Encoding.UTF8));
- Response.BinaryWrite(bytes);
- Response.Flush();
- Response.End();
- }
- }
|