在很多時候,上傳檔案是經常要用到的,一般我兩個方法,一種是通過ashx擴充,另一種說是通過wcf了,本篇只講述使用後者的實現方法。
現實功能:檔案上傳,簡單上傳進度顯示。
1.在asp.net工程裡建立項:Silverlight-enabled WCF Service
添加一個DoUpload方法:
- 1: [ServiceContract(Namespace = "")]
- 2: [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
- 3: public class Service1
- 4: {
- 5: [OperationContract]
- 6: public void DoUpload(string fileName, byte[] context, bool append)
- 7: {
- 8: //上傳目錄
- 9: string folder = System.Web.Hosting.HostingEnvironment.MapPath("~/upload");
- 10: if (!System.IO.Directory.Exists(folder))
- 11: {
- 12: //如果上傳目錄不存在則建立一個
- 13: System.IO.Directory.CreateDirectory(folder);
- 14: }
- 15: //檔案讀寫入模式
- 16: FileMode m = FileMode.Create;
- 17: if (append)
- 18: {
- 19: //如果參數為true則表示續傳,以追加模式操作檔案
- 20: m = FileMode.Append;
- 21: }
- 22:
- 23: //寫檔案操作
- 24: using (FileStream fs = new FileStream(folder + @"\" + fileName, m, FileAccess.Write))
- 25: {
- 26: fs.Write(context, 0, context.Length);
- 27: }
- 28: return;
- 29: }
- 30: }
複製代碼
2.引用我們的上述完成的wcf服務,silverlight工程右鍵->加入服務參考
image.png(63.54 K)2009-11-18 21:40:10
中點擊右邊的Discover按扭.
image_3.png(89.51 K)2009-11-18 21:40:10
中點擊OK按扭.注意:如出現錯誤,請先按F5鍵運行整個工程。
3.引用成功後就到sl裡的實現代碼了,為了簡單起見我只使用了一個button控制項:
MainPage.xaml
- 1: <UserControl x:Class="uploadFile.MainPage"
- 2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- 3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- 4: xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- 5: mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
- 6: <Grid x:Name="LayoutRoot">
- 7: <Button x:Name="bt" Content="UpLoad" Height="100" Width="200" />
- 8: </Grid>
- 9: </UserControl>
4.MainPage.xaml.cs,用戶端所有操作
- 1: public partial class MainPage : UserControl
- 2: {
- 3: public MainPage()
- 4: {
- 5: InitializeComponent();
- 6: }
- 7:
- 8: public MainPage()
- 9: {
- 10: InitializeComponent();
- 11: //註冊button的點擊事件
- 12: bt.Click += new RoutedEventHandler(bt_Click);
- 13: }
- 14:
- 15: //點擊事件
- 16: void bt_Click(object sender, RoutedEventArgs e)
- 17: {
- 18: //選擇本地檔案對話方塊
- 19: OpenFileDialog d = new OpenFileDialog();
- 20: //檔案過濾
- 21: d.Filter = "(*.*)|*.*";
- 22: //只能選單個檔案
- 23: d.Multiselect = false;
- 24: //選擇完成
- 25: if (d.ShowDialog() == true)
- 26: {
- 27: //檔案信消
- 28: FileInfo f = d.File;
- 29: //新執行個體化一個檔案從uploadfile類
- 30: uploadfile file = new uploadfile();
- 31: //檔案名稱
- 32: file.name = f.Name;
- 33: //檔案流內容
- 34: Stream s = f.OpenRead();
- 35: //檔案大小(/1000是為了方便查看,成為k為單位)
- 36: file.size = s.Length / 1000;
- 37: //執行個體file的內容
- 38: file.context = new List<byte[]>();
- 39:
- 40: //這裡讀取指定大小的檔案流內容到file.context準備上傳
- 41: int b;
- 42: while (s.Position > -1 && s.Position < s.Length)
- 43: {
- 44: if (s.Length - s.Position >= 300)
- 45: {
- 46: b = 3000;
- 47: }
- 48: else
- 49: {
- 50: b = (int)(s.Length - s.Position);
- 51: }
- 52:
- 53: byte[] filebyte = new byte[b];
- 54: s.Read(filebyte, 0, b);
- 55: file.context.Add(filebyte);
- 56: }
- 57: s.Close();
- 58:
- 59: //執行個體化wcf用戶端
- 60: ServiceReference1.uploadServiceClient uploader = new uploadFile.ServiceReference1.uploadServiceClient();
- 61: //註冊DoUpload完成事件
- 62: uploader.DoUploadCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(uploader_DoUploadCompleted);
- 63: //開始第一個包的上傳
- 64: uploader.DoUploadAsync(file.name, file.context[0], false, file);
- 65: }
- 66: }
- 67:
- 68: void uploader_DoUploadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
- 69: {
- 70: if (e.Error == null)
- 71: {
- 72: //上一個包上傳成功
- 73: uploadfile file = e.UserState as uploadfile;
- 74: //修改已上傳大小(顯示作用)
- 75: file.sent += file.context[0].Length / 1000;
- 76: //刪除已上傳內容
- 77: file.context.RemoveAt(0);
- 78: //如果上傳內容是空,完成操作
- 79: if (file.context.Count == 0)
- 80: {
- 81: bt.Content = "upload";
- 82: MessageBox.Show("upload OK");
- 83: }
- 84: else
- 85: {
- 86: //如果上傳內容不是空,則繼續剩餘下一段內容上傳
- 87: (sender as ServiceReference1.uploadServiceClient).DoUploadAsync(file.name, file.context[0], true, file);
- 88: //顯示進度
- 89: bt.Content = file.sent.ToString() + "/" + file.size.ToString();
- 90: }
- 91: }
- 92: }
- 93: }
- 94:
- 95: public class uploadfile
- 96: {
- 97: //檔案名稱
- 98: public string name { get; set; }
- 99: //檔案大小
- 100: public double size { get; set; }
- 101: //已上傳
- 102: public double sent { get; set; }
- 103: //上傳內容
- 104: public List<byte[]> context { get; set; }
- 105: }
文章轉載:http://www.pin5i.com/showtopic-26069-2.html