WPF文件上传及下载
来源:互联网 发布:java json对象转换map 编辑:程序博客网 时间:2024/06/05 23:12
WPF文件上传与下载功能 WPF版本与Silverlight版本文件的上传与下载功能有些不同接下来是主要代码: 后台代码: private void buttonedit_DefaultButtonClick(object sender, RoutedEventArgs e) { OpenFileDialog ofld = new OpenFileDialog(); if (ofld.ShowDialog() == System.Windows.Forms.DialogResult.OK) { this.buttonedit.Text = ofld.FileName; string s = System.IO.Path.GetFileNameWithoutExtension(ofld.FileName).ToString(); if (this.buttonedit.Text != "" || this.buttonedit.Text != null) { txtDocumentTitle.Text = s; } else { } } } ViewModel当中 if (this.FileName != null && this.FileName!="") { CurrentDocumentManage.FilePath = this.FileName; int istart = this.FileName.LastIndexOf('\\'); string sFileTitle = this.FileName.Substring(istart + 1); bool bIsTrue = UploadEnclosure(sServerPath, this.FileName); } /// /// 上传附件 /// /// 附件源地址 /// 附件上传地址 /// private bool UploadEnclosure(string sServerPath, string sEnclosurePath) { string sServerEnclousePath = ""; int i = sEnclosurePath.LastIndexOf("\\"); string sFileName = sEnclosurePath.Substring(i + 1); sServerEnclousePath = sServerPath + "\\EnclouseFolder\\" + sFileName; WebClient webclient = new WebClient(); webclient.Credentials = CredentialCache.DefaultCredentials; FileStream filestream = new FileStream(sEnclosurePath, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(filestream); byte[] uploadArray = br.ReadBytes((int)filestream.Length); Stream uploadStream = webclient.OpenWrite(sServerEnclousePath, "PUT"); if (uploadStream.CanWrite) { uploadStream.Write(uploadArray, 0, uploadArray.Length); uploadStream.Close(); return true; } return false; } /// /// 删除附件 /// /// 附件源地址 /// 附件名 private void DeleteEnclosure(string sServerPath, string sEnclosurePath) { string sServerEnclousePath = ""; int i = sEnclosurePath.LastIndexOf("\\"); string sFileName = sEnclosurePath.Substring(i + 1); sServerEnclousePath = sServerPath + "\\EnclouseFolder"; DirectoryInfo directory = new DirectoryInfo(sServerEnclousePath); FileInfo[] files = directory.GetFiles(); foreach (FileInfo file in files) { if (file.Name == sEnclosurePath) { string sPath = sServerEnclousePath + "\\" + sEnclosurePath; File.Delete(sPath); break; } } } 下载功能 private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DocumentManageService documentSerivce = new DocumentManageService(); string sFileName = ((TextBlock)sender).Text.ToString(); if (sFileName != "") { int iIndex = sFileName.LastIndexOf('.'); string sFilter = sFileName.Substring(iIndex + 1, sFileName.Length - iIndex - 1); System.Windows.Forms.SaveFileDialog sfdFile = new System.Windows.Forms.SaveFileDialog(); sfdFile.FileName = sFileName; sfdFile.Filter = sFilter + " files (*." + sFilter + ")|*." + sFilter; if (sfdFile.ShowDialog() == System.Windows.Forms.DialogResult.OK) { documentSerivce.SaveFile(sFileName, sfdFile.FileName); MessageBoxCollection.ShowOKMessageBox("下载成功!"); } } } public void SaveFile(string sFileTitle, string sFileCopyPath) { try { string sFilePath = System.Windows.Forms.Application.StartupPath + "\\EnclouseFolder\\" + sFileTitle; WebClient webclient = new WebClient(); webclient.Credentials = CredentialCache.DefaultCredentials; FileStream filestream = new FileStream(sFilePath, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(filestream); byte[] uploadArray = br.ReadBytes((int)filestream.Length); Stream uploadStream = webclient.OpenWrite(sFileCopyPath, "PUT"); if (uploadStream.CanWrite) { uploadStream.Write(uploadArray, 0, uploadArray.Length); uploadStream.Close(); } } catch { } }
0 0
- WPF文件上传及下载
- 文件上传及下载
- 文件上传及下载
- 文件上传及下载
- 文件上传及下载
- 文件上传及下载
- 文件下载及上传
- 文件上传及下载
- flex 文件上传及下载
- iOS 文件下载及上传
- javaweb文件上传及下载
- LoadRunner上传及下载文件
- struts 文件上传及文件下载
- 文件_文件的上传及下载
- 图片及文件上传和下载
- 图片及文件上传和下载
- Web Services上传及下载文件
- 使用LoadRunner上传及下载文件
- 常用字符串函数
- ffmpeg 在linux下编译
- Android视觉规范-间距规范与文字规范单位换算(dip、sp与px)
- error C2065: 'IDD_VIDEONETDLG' : undeclared identifier
- 有连接表的双向1-1关联
- WPF文件上传及下载
- stringByAppendingPathComponent和stringByAppendingString的区别
- ios开发 - 邮箱,手机号码,车牌号是否合法(正则表达)
- imgareaselect jquery.imgareaselect.js 页面图片插件
- final、static使用总结
- 根据国嵌的程序改的v4l2程序
- [iOS]UILabel改变某个范围的字符颜色和字体
- Android4.1.2 耳机插入拔出通知流程分析
- GIT使用小结(主要包括EGIT的使用,GIT命令的意义)