通过wcf 文件上传,简单上传进度显示

在很多时候,上传文件是经常要用到的,一般我两个方法,一种是通过ashx扩展,另一种说是通过wcf了,本篇只讲述使用后者的实现方法。

现实功能:文件上传,简单上传进度显示。

1.在asp.net工程里新建项:Silverlight-enabled WCF Service

添加一个DoUpload方法:
  1. 1:  [ServiceContract(Namespace = "")]
  2. 2:  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  3. 3:  public class Service1
  4. 4:  {
  5. 5:      [OperationContract]
  6. 6:      public void DoUpload(string fileName, byte[] context, bool append)
  7. 7:      {
  8. 8:          //上传目录
  9. 9:          string folder = System.Web.Hosting.HostingEnvironment.MapPath("~/upload");
  10. 10:          if (!System.IO.Directory.Exists(folder))
  11. 11:          {
  12. 12:              //如果上传目录不存在则新建一个
  13. 13:              System.IO.Directory.CreateDirectory(folder);
  14. 14:          }
  15. 15:          //文件读写模式
  16. 16:          FileMode m = FileMode.Create;
  17. 17:          if (append)
  18. 18:          {
  19. 19:              //如果参数为true则表示续传,以追加模式操作文件
  20. 20:              m = FileMode.Append;
  21. 21:          }
  22. 22: 
  23. 23:          //写文件操作
  24. 24:          using (FileStream fs = new FileStream(folder + @"\" + fileName, m, FileAccess.Write))
  25. 25:          {
  26. 26:              fs.Write(context, 0, context.Length);
  27. 27:          }
  28. 28:          return;
  29. 29:      }
  30. 30:  }
复制代码
2.引用我们的上述完成的wcf服务,silverlight工程右键->添加服务引用



上图中点击右边的Discover按扭.



上图中点击OK按扭.注意:如出现错误,请先按F5键运行整个工程。

3.引用成功后就到sl里的实现代码了,为了简单起见我只使用了一个button控件:

MainPage.xaml
  1. 1:  <UserControl x:Class="uploadFile.MainPage"
  2. 2:      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. 3:      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. 4:      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. 5:      mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  6. 6:      <Grid x:Name="LayoutRoot">
  7. 7:          <Button x:Name="bt" Content="UpLoad" Height="100" Width="200" />
  8. 8:      </Grid>
  9. 9:  </UserControl>
赞助商
4.MainPage.xaml.cs,客户端所有操作
  1.   1:  public partial class MainPage : UserControl
  2.   2:  {
  3.   3:      public MainPage()
  4.   4:      {
  5.   5:          InitializeComponent();
  6.   6:      }
  7.   7: 
  8.   8:      public MainPage()
  9.   9:      {
  10. 10:          InitializeComponent();
  11. 11:          //注册button的点击事件
  12. 12:          bt.Click += new RoutedEventHandler(bt_Click);
  13. 13:      }
  14. 14: 
  15. 15:      //点击事件
  16. 16:      void bt_Click(object sender, RoutedEventArgs e)
  17. 17:      {
  18. 18:          //选择本地文件对话框
  19. 19:          OpenFileDialog d = new OpenFileDialog();
  20. 20:          //文件过滤
  21. 21:          d.Filter = "(*.*)|*.*";
  22. 22:          //只能选单个文件
  23. 23:          d.Multiselect = false;
  24. 24:          //选择完成
  25. 25:          if (d.ShowDialog() == true)
  26. 26:          {
  27. 27:              //文件信消
  28. 28:              FileInfo f = d.File;
  29. 29:              //新实例化一个文件从uploadfile类
  30. 30:              uploadfile file = new uploadfile();
  31. 31:              //文件名
  32. 32:              file.name = f.Name;
  33. 33:              //文件流内容
  34. 34:              Stream s = f.OpenRead();
  35. 35:              //文件大小(/1000是为了方便查看,成为k为单位)
  36. 36:              file.size = s.Length / 1000;
  37. 37:              //实例file的内容
  38. 38:              file.context = new List<byte[]>();
  39. 39: 
  40. 40:              //这里读取指定大小的文件流内容到file.context准备上传
  41. 41:              int b;
  42. 42:              while (s.Position > -1 && s.Position < s.Length)
  43. 43:              {
  44. 44:                  if (s.Length - s.Position >= 300)
  45. 45:                  {
  46. 46:                      b = 3000;
  47. 47:                  }
  48. 48:                  else
  49. 49:                  {
  50. 50:                      b = (int)(s.Length - s.Position);
  51. 51:                  }
  52. 52: 
  53. 53:                  byte[] filebyte = new byte[b];
  54. 54:                  s.Read(filebyte, 0, b);
  55. 55:                  file.context.Add(filebyte);
  56. 56:              }
  57. 57:              s.Close();
  58. 58: 
  59. 59:              //实例化wcf客户端
  60. 60:              ServiceReference1.uploadServiceClient uploader = new uploadFile.ServiceReference1.uploadServiceClient();
  61. 61:              //注册DoUpload完成事件
  62. 62:              uploader.DoUploadCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(uploader_DoUploadCompleted);
  63. 63:              //开始第一个包的上传
  64. 64:              uploader.DoUploadAsync(file.name, file.context[0], false, file);
  65. 65:          }
  66. 66:      }
  67. 67: 
  68. 68:      void uploader_DoUploadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
  69. 69:      {
  70. 70:          if (e.Error == null)
  71. 71:          {
  72. 72:              //上一个包上传成功
  73. 73:              uploadfile file = e.UserState as uploadfile;
  74. 74:              //修改已上传大小(显示作用)
  75. 75:              file.sent += file.context[0].Length / 1000;
  76. 76:              //删除已上传内容
  77. 77:              file.context.RemoveAt(0);
  78. 78:              //如果上传内容是空,完成操作
  79. 79:              if (file.context.Count == 0)
  80. 80:              {
  81. 81:                  bt.Content = "upload";
  82. 82:                  MessageBox.Show("upload OK");
  83. 83:              }
  84. 84:              else
  85. 85:              {
  86. 86:                  //如果上传内容不是空,则继续剩余下一段内容上传
  87. 87:                  (sender as ServiceReference1.uploadServiceClient).DoUploadAsync(file.name, file.context[0], true, file);
  88. 88:                  //显示进度
  89. 89:                  bt.Content = file.sent.ToString() + "/" + file.size.ToString();
  90. 90:              }
  91. 91:          }
  92. 92:      }
  93. 93:  }
  94. 94: 
  95. 95:  public class uploadfile
  96. 96:  {
  97. 97:      //文件名
  98. 98:      public string name { get; set; }
  99. 99:      //文件大小
  100. 100:      public double size { get; set; }
  101. 101:      //已上传
  102. 102:      public double sent { get; set; }
  103. 103:      //上传内容
  104. 104:      public List<byte[]> context { get; set; }
  105. 105:  }
 文章转载:http://www.pin5i.com/showtopic-26069-2.html
原文地址:https://www.cnblogs.com/star250/p/1758586.html