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
原创粉丝点击