private void btnInvokeByHand_Click(object sender, EventArgs e) { string fileName = SelectFile(); if (string.IsNullOrEmpty(fileName)) { return; } this.Cursor = Cursors.WaitCursor; FileStream stream = File.OpenRead(fileName); var req = new FileUploadMessage(fileName.Substring(fileName.LastIndexOf('\\') + 1), "", stream); BasicHttpBinding binding = new BasicHttpBinding(); binding.TransferMode = TransferMode.Streamed; binding.MessageEncoding = WSMessageEncoding.Mtom; binding.MaxReceivedMessageSize = 9223372036854775807; binding.SendTimeout = new TimeSpan(0, 0, 10, 0); // 设置十分钟超时 IUpLoadService channel = ChannelFactory <IUpLoadService> .CreateChannel(binding, new EndpointAddress("http://localhost:62805/UpLoadService.svc")); using (channel as IDisposable) { channel.UploadFile(req); stream.Close(); this.Cursor = Cursors.Default; MessageBox.Show("文件上传到服务器成功", "上传WCF", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
static void Main(string[] args) { AppDomain.CreateDomain("Server").DoCallBack(delegate { ServiceHost host = new ServiceHost(typeof(UpLoadService), new Uri("http://localhost:1630/UpLoadService.svc")); BasicHttpBinding binding = new BasicHttpBinding(); binding.TransferMode = TransferMode.Streamed; binding.MaxReceivedMessageSize = 67108864; host.AddServiceEndpoint(typeof(IUpLoadService), binding, ""); host.Open(); }); BasicHttpBinding binding2 = new BasicHttpBinding(); binding2.TransferMode = TransferMode.Streamed; IUpLoadService channel = ChannelFactory <IUpLoadService> .CreateChannel(binding2, new EndpointAddress("http://localhost:1630/UpLoadService.svc")); using (channel as IDisposable) { FileUploadMessage file = new FileUploadMessage(); file.SavePath = "ppp"; file.FileName = "safs.bak"; file.FileData = new FileStream("D:/work/C#/lll.bak", FileMode.Open); channel.UploadFile(file); file.FileData.Close(); } }
private void btnDownload2_Click(object sender, EventArgs e) { string fileName = SelectFile(); if (string.IsNullOrEmpty(fileName)) { return; } this.Cursor = Cursors.WaitCursor; DownFileRequest req = new DownFileRequest(fileName); BasicHttpBinding binding = new BasicHttpBinding(); binding.TransferMode = TransferMode.Streamed; binding.MessageEncoding = WSMessageEncoding.Mtom; binding.MaxReceivedMessageSize = 9223372036854775807; IUpLoadService channel = ChannelFactory <IUpLoadService> .CreateChannel(binding, new EndpointAddress("http://localhost:62805/UpLoadService.svc")); using (channel as IDisposable) { var res = channel.DownLoadFile(req); if (res.IsSuccess) { using (var fileStream = File.Create(AppDomain.CurrentDomain.BaseDirectory + "\\" + fileName.Substring(fileName.LastIndexOf('\\') + 1))) { CopyTo(res.FileStream, fileStream); } } this.Cursor = Cursors.Default; MessageBox.Show(res.IsSuccess ? "文件下载成功!" : res.Message, "上传文件测试", MessageBoxButtons.OK, res.IsSuccess ? MessageBoxIcon.Information : MessageBoxIcon.Error); } }