同步文件的做法

所有的逻辑都是在wcf处理的

所以文件保存也是在wcf端

同步也就是说将wcf保存的文件复制到webUI下面


代码:


 /// <summary>
        /// 文件同步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bbi_sync_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                string[] fileNames = { };
                string sourceFiles = InvokeToWCF.ClientInvokeToServerBLL(
               "FilesManagerBLL", "GetAllFile", "GET");
                fileNames = sourceFiles.Split(',');



                WebClient toHandler = new WebClient();
                StringBuilder sb = new StringBuilder();

                //string files = string.Join(",", fileNames);
                sb.Append("Type=Sync");
                sb.Append("&FileNames=" + sourceFiles);
                byte[] bytes = Encoding.UTF8.GetBytes(sb.ToString());
                toHandler.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                toHandler.Headers.Add("ContentLength", bytes.Length.ToString());
                byte[] returndata = toHandler.UploadData("http://localhost:3395/Test/DownloadHandler.ashx", "POST", bytes);
                string strcheck = Encoding.UTF8.GetString(returndata);
                string[] strchecks = strcheck.Split('|');
                if (strchecks[0] == "Sync Complete" && strchecks[1] == string.Empty)
                {
                    MessBox.MessBoxInfo("同步完成!");
                }
                else if (strchecks[0] == "Sync Complete" && strchecks[1] != string.Empty)
                {
                    MessBox.MessBoxInfo("同步完成!\n但是缺失了文件:\n" + strchecks[1].ToString());
                }
            }
            catch (Exception ex)
            {
                MessBox.MsgBoxErr(ex);
            }
        }

DownloadHandler.ashx代码

public void ProcessRequest(HttpContext context)
        {
            string type = context.Request["Type"];
            // 同步文件
            if (type == "Sync")
            {
                string files = context.Request["FileNames"];
                SyncFiles(files, context);
            }
            else if (type == "save")
            {
                string _context = context.Request["content"].ToString().Trim().Replace("%2B", "+");
                string file = context.Request["file"];
                string _path = context.Server.MapPath("~/Upload/") + file.Replace('/', '\\');
                WriteFile(_path, _context, context);
            }

        }

        /// <summary>
        /// 同步文件
        /// </summary>
        public void SyncFiles(string _files, HttpContext _context)
        {
            string sourceCode = InvokeToWCF.ClientInvokeToServerBLL(
                "FilesManagerBLL", "DownloadAllFiles", "GET", _files);
            JObject jobj = JObject.Parse(sourceCode);
            string[] fileNames = _files.Split(',');
            string path = AppDomain.CurrentDomain.BaseDirectory;
            for (int i = 0; i < fileNames.Length; i++)
            {
                string key = fileNames[i];
                if (jobj[key] != null)
                {
                    string fullpath = path + fileNames[i].Replace('/', '\\');
                    using (FileStream fs = new FileStream(fullpath, FileMode.OpenOrCreate, FileAccess.Write))
                    {

                        byte[] info = System.Convert.FromBase64String(jobj[key].ToString());
                        fs.Write(info, 0, info.Length);
                        fs.Close();

                    }
                }
            }
            string loseFiles = jobj["loseFiles"].ToString();
            _context.Response.Write("Sync Complete|" + loseFiles);
        }


        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="context">内容</param>
        public static void WriteFile(string _path, string _context, HttpContext context)
        {
            try
            {
                using (FileStream fs = new FileStream(_path, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    byte[] info = Convert.FromBase64String(_context);
                    fs.Write(info, 0, info.Length);
                    fs.Close();
                }
                context.Response.Write("save complete");
            }
            catch (Exception)
            {
                throw;
            }
        }



逻辑层代码

FileManageBll   class



#region 成员变量
        //ISTA_DOC_FILE
        private ISTA_DOC_FILE ista_doc_file { get; set; }
        #endregion

        #region 构造函数
        public FilesManagerBLL(ISTA_DOC_FILE ista_doc_file)
        {
            this.ista_doc_file = ista_doc_file;
        }
        #endregion

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="fileName">文件名</param>
        public string DownloadFile(string fileName)
        {
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory;
                // 宿主程序集名
                // string hostAssemblyName = "EPS.Budget.WCF.Host";
                // .Remove(path.IndexOf(hostAssemblyName)) + hostAssemblyName
                path = path + "Files\\" + fileName;
                return ReadFile(path);
            }
            catch (Exception ex)
            {
                throw new Exception("业务逻辑层错误", ex);
            }
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="fileName">文件名</param>
        public string DownloadFileByPath(string _fileName, string _path)
        {
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory;
                path = path + _path + _fileName;
                return ReadFile(path);
            }
            catch (Exception ex)
            {
                throw new Exception("业务逻辑层错误", ex);
            }
        }

        /// <summary>
        /// 读文件
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns></returns>
        public static string ReadFile(string path)
        {
            try
            {
                string strFile = "";
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    byte[] b = new byte[fs.Length];
                    fs.Read(b, 0, b.Length);
                    strFile = System.Convert.ToBase64String(b);
                    fs.Close();
                }
                return strFile;
            }
            catch (Exception ex)
            {
                throw new Exception("业务逻辑层错误", ex);
            }
        }

        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="_name"></param>
        /// <param name="_context"></param>
        public void SaveFile(string _fileName, string _context, string _path)
        {
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory;
                path = path + "Upload\\" + _path + "\\" + _fileName;
                using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    byte[] info = Convert.FromBase64String(_context);
                    fs.Write(info, 0, info.Length);
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("业务逻辑层错误", ex);
            }
        }

        /// <summary>
        /// 下载所有文件
        /// </summary>
        /// <returns></returns>
        public string DownloadAllFiles(string _files)
        {
            try
            {
                JObject jobj = new JObject();
                string[] fileNames = _files.Split(',');
                string path = AppDomain.CurrentDomain.BaseDirectory;
                StringBuilder loseFileName = new StringBuilder();
                for (int i = 0; i < fileNames.Length; i++)
                {
                    string fullpath = path + fileNames[i].Replace('/', '\\');
                    if (File.Exists(fullpath))
                    {
                        string stream = ReadFile(fullpath);
                        jobj.Add(new JProperty(fileNames[i], stream));
                    }
                    else
                    {
                        loseFileName.Append(fileNames[i]).Append("\n");//.Append(",");
                    }
                }
                jobj.Add(new JProperty("loseFiles", loseFileName.Remove(loseFileName.ToString().Length - 1, 1).ToString()));
                return jobj.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("业务逻辑层错误", ex);
            }
        }


        /// <summary>
        /// 下载所有文件
        /// </summary>
        /// <returns></returns>
        public string GetAllFile()
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                List<STA_DOC_FILEInfo> list = ista_doc_file.GetAll();
                foreach (STA_DOC_FILEInfo item in list)
                {
                    sb.Append(item.FILELJ + item.FILEBH + item.FILEMC + item.FILEEX).Append(",");
                }
                sb.Remove((sb.ToString()).Length - 1, 1);
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("业务逻辑层错误", ex);
            }
        }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值