同步文件的做法
来源:互联网 发布:淘宝复制别人店铺模板 编辑:程序博客网 时间:2024/05/17 04:39
所有的逻辑都是在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); } }
0 0
- 同步文件的做法
- SAP销售发票同步产生会计凭证的两种做法
- 读取Properties文件的一般做法
- 导出文件后,打开目录的做法
- android可执行sql文件的做法
- jsp下载excel文件的一般做法
- 日志文件支持unicode字符的做法
- 关于不同屏幕加载不同的布局文件的做法
- pugixml读取unicode编码的xml文件的做法
- 文件操作常用做法
- 网站文件的同步
- SQL Server中读取XML文件的简单做法
- SQL Server中读取XML文件的简单做法
- 将java程序做成exe文件的几种做法
- Matlab中从文件夹中批量读取文件的做法
- SQL Server中读取XML文件的简单做法
- 关于去除PE文件中函数修饰的做法
- 不使用资源文件动态创建对话框的做法
- 10 个新鲜的Web设计和开发资源列表
- 纯CSS实现表单验证
- Shader Forge可视化Unity3D Shader编辑器二-基础着色器
- 线程:实现Runnable车站卖票,但是代码还不成熟
- StaggeredGridView 瀑布流 实现,超简单的方式
- 同步文件的做法
- 寒假之作—HDU—交换座位问题
- 创建 Windows 8 Metro UI 风格网站所需要的资源集合
- 地理空间距离计算优化
- 关于IOS delegate回调的理解
- Too many arguments to function call
- 用JQuery Mobile做HTML5移动应用的三个优缺点
- 菜鸟的Linux学习笔记之命令shutdown
- 调用MyReport实现js直接打印